BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wernie
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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;
ballardw
Super User

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.

sdhilip
Quartz | Level 8

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 5080 views
  • 3 likes
  • 4 in conversation