I need to extract words before a delimiter (comma) and can't figure out how to do it. In some cases it's one word and in other cases it's more than one word.
For example, I have:
Clay County, AL
Clark County, AL
La Paz County, AZ
Santa Cruz County, AZ
So for each of those, I want to extract all of the words before the comma. Thanks!
data have;
input words $30.;
cards;
Clay County, AL
Clark County, AL
La Paz County, AZ
Santa Cruz County, AZ
;
data want;
set have;
want=scan(words,1,',');
run;
data have;
input words $30.;
cards;
Clay County, AL
Clark County, AL
La Paz County, AZ
Santa Cruz County, AZ
;
data want;
set have;
want=scan(words,1,',');
run;
One way:
data example; set have; city = scan(variable,1,','); state= scan(variable,2,','); run;
SCAN function will extract "words" from a variable, with the number 1,2, 3 etc indicating which "word" and the last parameter is delimiter character(s). By default the function would use space and a few other characters but you can limit to a single delimiter character by specifying in the third position.
You did not mention the name of your existing variable so...
If you know how long you want the resulting variable to be then best is to provide a LENGTH statement before extracting. Otherwise the variables are likely to inherit the length of the source variable which may lead to wasted disk space use.
Hi
data have;
input text$30.;
datalines;
Clay County, AL
Clark County, AL
La Paz County, AZ
Santa Cruz County, AZ
;
run;
data want (drop= text);
set have;
City = scan(text, 1, ',');
State = scan(text, 2, ',');
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.