Return to Archive

SAS Tip of the Month
May 2003

The INDEX functions are very useful in finding out if a character or word exists in the string. The documentation gives these functions with the following definition:

INDEX -- Searches a character expression for a string of characters
INDEXC -- Searches a character expression for specific characters
INDEXW -- Searches a character expression for a specified string as a word

Now lets see how them in use with the following examples:

   252 data _null_;
   253 TextStr='Apt 56B, Whitehaven Mansions, Sandhurst Sq, London';
   254
   255 *** The location of the first string '6B';
   256 FindINDEX=index(TextStr,'6B');
   257
   258 *** The location of first 's' or 'a' (regardless of order)';
   259 FindINDEXC=indexc(TextStr,'sa');
   260
   261 *** The location of first word 'Whitehaven';
   262 FindINDEXW=indexw(TextStr,'Whitehaven');
   263
   264 put TextStr= /
   265 FindINDEX= FindINDEXC= FindINDEXW=;
   266 run;

   TextStr=Apt 56B, Whitehaven Mansions, Sandhurst Sq, London
   FindINDEX=6 FindINDEXC=16 FindINDEXW=10
________________________________
Updated May 1, 2003