SCAN(string,n,delimiters): returns the nth word from the character string string, where words are delimited by the characters in delimiters.
It is used to extract words from a character value when the relative order of words is known, but their starting positions are not.
NewVar=SCAN(string,n<,delimiters>); -returns the nth ‘word’ in the string
When the SCAN function is used:
the length of the created variable is 200 bytes if it is not previously defined with a LENGTH statement
delimiters before the first word have no effect When the SCAN function is used,
any character or set of characters can serve as delimiters
Points to remember while using SCAN Function:
a missing value is returned if there are fewer than n words in string
two or more contiguous delimiters are treated as a single delimiter a missing value is returned if there are fewer than n words in string
if n is negative, SCAN selects the word in the character string starting from the end of string.
If you omit delimiters , default is blank . < ( + & ! $ * ) ; ^ - / , %
Source: http://www.biostat.jhsph.edu/bstcourse/bio632/SummerInst/Class2/class2.pdf
Example of use:
Last= scan(Name,2,','); *Note : Comma ',' is Delimiter here;
Results in Last returns to Blog
Using the SCAN function from SAS Functions by Example:
Suppose you want to produce an alphabetical list by last name, but your NAME variable contains FIRST, possibly a middle initial, and LAST name. The SCAN function makes quick work of this. Note that the LAST_NAME variable in PROC REPORT has the attribute of ORDER and NOPRINT, so that the list is in alphabetical order of last name but all that shows up is the original NAME variable in First, Middle, and Last name order.
DATA FIRST_LAST;
INPUT @1 NAME $20.@21 PHONE $13.;
INPUT @1 NAME $20.@21 PHONE $13.;
***Extract the last name from NAME;
LAST_NAME = SCAN(NAME,-1,' '); /* Scans from the right */
DATALINES;
Jeff W. Snoker (908)782-4382
Raymond Albert (732)235-4444
Steven J. Foster (201)567-9876
Jose Romerez (516)593-2377
;
PROC REPORT DATA=FIRST_LAST NOWD;
TITLE "Names and Phone Numbers in Alphabetical Order (by Last Name)";
COLUMNS NAME PHONE LAST_NAME;
DEFINE LAST_NAME / ORDER NOPRINT WIDTH=20;
DEFINE NAME / DISPLAY 'Name' LEFT WIDTH=20;
DEFINE PHONE / DISPLAY 'Phone Number' WIDTH=13 FORMAT=$13.;
RUN;
source: www.support.sas.com