Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Finding a Words Start Position

The SCAN function provides a flexible way of finding the nth word in a character string, by allowing searches to be conducted in either direction along the string and through the specification of word delimiters. How though would we find the start position of the nth word in a string?

Unfortunately the SCAN function does not provide a means of capturing this information. We could attempt to solve this problem using an INDEX or FIND function but what if the nth word occurs multiple times?

The solution is to use the CALL SCAN routine which will not only return the position number but also the length of the word in question.

The following example returns these details for the 9th word (which is 'a') in the string. Note how leading blanks are included when calculating the position.

data _null_;
  string = ' This is a long character string with seemingly a 
		lot of words to search for';
  call scan(string,9,position,length);
  put position=;
  put length=;
run;
position=49 length=1

As with the SCAN function, the second argument can be positive to search from left to right or negative to search from right to left. For example, entering a second argument of -1 would identify the position and length of the last word in a string.

CALL SCAN also supports a series of modifiers that affect the search. In the following example we have a string which should contain seven pieces of information but only contains five. If we ask CALL SCAN to return the fifth word it will, by default, treat multiple consecutive delimiters as one and return the length and position of the word 'Married'. Specifying the 'M' modifier, forces SAS to honour consecutive delimiters.

data _null_;
  string = 'Bob, Robertson, Male, 29,,,Married';
  call scan(string,5,position,length,’M’);
  put position=;
  put length=;
run;
position=0