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;
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;