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
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.
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.
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);
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?
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?
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.
Awesome!!!!
Index may not be the function to use. Index may find the Apt in "CAptain Jones St". You may want to look at INDEXW
Good add up!
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.
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.