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
note to the folks playing along at home -- make sure you pad the datalines in this code so that the phone number starts at column 21. it won't work right if you copy and paste it directly (like i just did...). :-P
ReplyDeleteHi,
ReplyDeletea google search for 'sas scan alphabetical' led me to your blog.
which me to ask a question regarding scan function. is there anyway for scan to return an alphabetical component listing.
e.g.,
the study is to list all cars at a specific 4-way stoplight on a multi-lane road (pure fictional exmple!)
variable is 'CARS'
data lines listed as CARS looks like this:
chevy, ford, chyrsler, alpha romeo;
toyota, mazada, studebaker, ;
ford, ford, chevy, BMW;
etc..
car1 = scan(CARS,1,",");
car2 = scan(CARS,2,",");
car3 = scan(CARS,3,",");
car4 = scan(CARS,4,",");
etc..
what I would like is the have scan do is to ensure that 'car1' always has the alphabetical first from the list of cars in the multi-listed variable CARS.
so results would look like this:
cars1 cars2 cars3 cars4
alpha romeo chevy chrysler ford
mazada studebaker toyota
BMW chevy ford ford
thanks
shan
shan8299@gmail.com
Seems like it won't work when the delimiter is more than one character?
ReplyDeleteHi Shan,
ReplyDeleteI don't know how to sort the values using the SCAN function. But using the Call Sortc routine you can do this.
Here is the code.
data test;
length cars $200;
input cars $1-50;
cards;
chevy, ford, chyrsler, alpha romeo,
toyota, mazada, studebaker,
ford, ford, chevy, BMW,
;
run;
data test1;
set test;
car1 = scan(CARS,1,",");
car2 = scan(CARS,2,",");
car3 = scan(CARS,3,",");
car4 = scan(CARS,4,",");
array x(*) $ car1 car2 car3 car4;
call sortc(of x(*));
run;
Let me know if you have any questions.
Sarath
How I shoul replace a char with num and num with char in a given example Variable date contains observations
ReplyDelete01:unk:2009
unk:01 2010
Unk shud replace with 'jan' in 1st observation
Unk in 2 nd observation shud replace by 15
Usin char function
Can anybody help please
The default length of the return value of SCAN is actually the length of the first argument, rather than 200. (This is different from the default behavior of the CAT family of functions, where the length of the returned argument is indeed 200.)
ReplyDelete