DATA Step, Macro, Functions and more

Separate Apt information from Street information

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Separate Apt information from Street information

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


Accepted Solutions
Solution
‎05-31-2017 03:36 PM
Super User
Posts: 5,503

Re: Separate Apt information from Street information

Posted in reply to fannavivian

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


All Replies
Super User
Posts: 5,426

Re: Separate Apt information from Street information

Posted in reply to fannavivian
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
Solution
‎05-31-2017 03:36 PM
Super User
Posts: 5,503

Re: Separate Apt information from Street information

Posted in reply to fannavivian

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.

Occasional Contributor
Posts: 13

Re: Separate Apt information from Street information

[ Edited ]
Posted in reply to Astounding

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);

Super User
Posts: 5,503

Re: Separate Apt information from Street information

Posted in reply to fannavivian

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?

Occasional Contributor
Posts: 13

Re: Separate Apt information from Street information

Posted in reply to Astounding

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?

Super User
Posts: 5,503

Re: Separate Apt information from Street information

Posted in reply to fannavivian

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.

Occasional Contributor
Posts: 13

Re: Separate Apt information from Street information

Posted in reply to Astounding

Awesome!!!!

Super User
Posts: 11,343

Re: Separate Apt information from Street information

Posted in reply to fannavivian

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

Occasional Contributor
Posts: 13

Re: Separate Apt information from Street information

Good add up!

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 199 views
  • 2 likes
  • 4 in conversation