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

Hey everyone,

 

I have a bit of a problem extracting certain words from a string to make new variables.  I know that the scan function can normally be used to solve this problem quite easily, but the problem I have is that some of the entries for the new variable contain more that one word.

 

For example (using an address):  123 Main Street, Los Angeles, California, 11000

                                                      123 North Main Street, Los Angeles, California, 11000.

 

As is stands right now, those addresses are lumped into one variable.  I want to extract the strings so I have a distinct variable for: Street, City, State, and Zip Code.  The problem with using a basic SCAN function is that streets with two-word names are not captured correctly.  Does anyone have a solution??

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Look at the parameters in SCAN() function.

Specifically specifying the delimiter. If it's as in your example, then use the comma as your delimiter in the SCAN function.

The 'basic' scan function will work 🙂

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

Look at the parameters in SCAN() function.

Specifically specifying the delimiter. If it's as in your example, then use the comma as your delimiter in the SCAN function.

The 'basic' scan function will work 🙂

 

 

ad123123
Fluorite | Level 6

Hi,

 

Assuming that strings are given in the same fashion as mentioned in your problem statement,  following code will help:-

data have;
length add $ 1000;
add='123 Main Street, Los Angeles, California, 11000';
output;
add='123 North Main Street, Los Angeles, California, 11000';
output;
run;

data want;
set have;
house_no=substr(add,1,3);
street=substr(strip(scan(add,1,',')),4);
city=strip(scan(add,2,','));
state=strip(scan(add,3,','));
zipcode=strip(scan(add,4,','));
run;

Please let me know if it helps.

 

Regards,

Abd.

 

DFlynn24
Calcite | Level 5

Reeza,

 

You were certainly correct, forgot that I could specify a comma delimiter in the SCAN function.  Thanks for your help!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 882 views
  • 0 likes
  • 3 in conversation