DATA Step, Macro, Functions and more

Splitting a String

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Splitting a String

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??


Accepted Solutions
Solution
‎02-17-2016 03:12 PM
Super User
Posts: 17,829

Re: Splitting a String

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 Smiley Happy

 

 

View solution in original post


All Replies
Solution
‎02-17-2016 03:12 PM
Super User
Posts: 17,829

Re: Splitting a String

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 Smiley Happy

 

 

Occasional Contributor
Posts: 18

Re: Splitting a String

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.

 

Occasional Contributor
Posts: 5

Re: Splitting a String

Reeza,

 

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 260 views
  • 0 likes
  • 3 in conversation