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

Hi Guys,

 

Got a question about separating Apt information from address. The data looks like this:

 

Address

5213 Elmcrofa Blvd Apt 10210

5648 Sunset Blvd Apt 52103

...

I was thinking about using scan function, but not sure if the delimiter can be a character and how. The desired output would be like this:

 

Address                               Apt

5213 Elmcrofa Blvd             Apt 10210

5648 Sunset Blvd                Apt 52103

 

Thanks,

Fan

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

If you can rely on "Apt" indicating where to separate the text, this would work:

 

data want;

set have;

start = index(address, 'Apt');

if start = 1 then do;

   apartment = address;

   address = ' ';

end;

else if start > 1 then do;

   apartment = substr(address, start);

   address = substr(address, 1, start-1);

end;

run;

 

If you have to consider other separators, such as 'apt' or 'APT' or 'apmnt', it becomes more detailed but can use pretty much the same tools.

View solution in original post

9 REPLIES 9
LinusH
Tourmaline | Level 20
Street addresses are often more or less free text fields, which make them hard to parse by simple rules.
There have been several discussions on the forum regarding this, feel free to search and explore the hidden treasures of SAS Communities!
Data never sleeps
Astounding
PROC Star

If you can rely on "Apt" indicating where to separate the text, this would work:

 

data want;

set have;

start = index(address, 'Apt');

if start = 1 then do;

   apartment = address;

   address = ' ';

end;

else if start > 1 then do;

   apartment = substr(address, start);

   address = substr(address, 1, start-1);

end;

run;

 

If you have to consider other separators, such as 'apt' or 'APT' or 'apmnt', it becomes more detailed but can use pretty much the same tools.

fannavivian
Obsidian | Level 7

Thank you~~ This is similar to what I thought, but yours is more detailed. Just to share what I figured out a moment ago.

 

address=substr(ADDRESS1, 1, index(ADDRESS1, 'Apt') - 1);

Astounding
PROC Star

Always good to experiment and learn, but here are some things to be wary of.

 

Functions take some time to run.  It's faster to use INDEX once per DATA step instead of twice.

 

If "Apt" does not appear, INDEX will return 0.  Will the program still work in that case?

fannavivian
Obsidian | Level 7

Thanks. I'm not worry about the case you just mentioned, because the first left letter of address is always a number in my db. But I do have a question that what if there're other delimitors, such as "suite", "#", "STE", so what's your recommendation?

Astounding
PROC Star

Using the code I posted originally, an easy change would be:

 

start = max( index(address, 'Apt'), index(address, '#'), index(address, 'STE'), index(address, 'suite') );

 

If you find other possible separators, it's easy enough to add to the list.

ballardw
Super User

Index may not be the function to use. Index may find the Apt in "CAptain Jones St". You may want to look at INDEXW

fannavivian
Obsidian | Level 7

Good add up!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 3655 views
  • 2 likes
  • 4 in conversation