I'm currently working on Geocoding addresses to census tract and I'm able to delete addresses with 'APT' followed by the number when its on the far right, however some of my data begins with APT X/APT XX/APT XXX. For example:
APT 6 651 DALE ST
APT 2618 1214 OLD LASCASSAS RD
APT 305 8384 RIVER FORK
APT #D UNIVERSITY ST
Is there anyway to get ride of the APT along with whatever is followed up after?
address=left(tranwrd(address,'APT ',''));
nextword=scan(address,1);
address=substr(address,length(nextword)+2);
if address=:'APT ' then delete;
I tried as both of you suggested however it did not work. It deletes some of my observations and it did not change the result. Here is my data and here is what I am trying to do.
Have:
APT 6 651 DALE ST
APT 2618 1214 OLD LASCASSAS RD
APT 305 8384 RIVER FORK
APT #D UNIVERSITY ST
Want:
651 DALE ST
1214 OLD LASCASSAS RD
8384 RIVER FORK
UNIVERSITY ST
Here is my code for eliminating addresses that have APT and number at the end:
data out.noapt1;
set out.zip;
new_address =upcase(streetc);
pos = indexw(new_address, 'APT');
if pos > 0 then new_address = substr(new_address,1, pos-2);
run;
@IsaacYi881 wrote:
I tried as both of you suggested however it did not work. It deletes some of my observations and it did not change the result. Here is my data and here is what I am trying to do.
Have:
APT 6 651 DALE ST
APT 2618 1214 OLD LASCASSAS RD
APT 305 8384 RIVER FORK
APT #D UNIVERSITY ST
Want:
651 DALE ST
1214 OLD LASCASSAS RD
8384 RIVER FORK
UNIVERSITY ST
Here is my code for eliminating addresses that have APT and number at the end:
data out.noapt1;
set out.zip;
new_address =upcase(streetc);
pos = indexw(new_address, 'APT');
if pos > 0 then new_address = substr(new_address,1, pos-2);
run;
address=tranwrd(address,'APT ','');
nextword=scan(address,1);
address=substr(address,length(nextword)+1);
So getting closer, the code cuts off parts of the address. So when I run that code I'm getting
Address:
6 651 DATE DT
8 1214 OLD LASCASSAS RD
5 8384 RIVER FORK
nextword:
6
2618
305
could you please try the below tested code
data want;
input address&$100.;
if address=prxchange('s/^(APT)(.*)/$2/i',-1,address);
cards;
APT 6 651 DALE ST
APT 2618 1214 OLD LASCASSAS RD
APT 305 8384 RIVER FORK
APT #D UNIVERSITY ST
;
I tried this code and this is the log I get:
970 data practice2;
971 set out.noapt1;
972 new_address=prxchange('s/^(APT)(.*)/$2/i',new_address);
---------
71
ERROR 71-185: The PRXCHANGE function call does not have enough arguments.
973 run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
972:43
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.PRACTICE2 may be incomplete. When this step was stopped there were 0 observations and 27 variables.
NOTE: DATA statement used (Total process time):
real time 0.08 seconds
cpu time 0.07 seconds
Sorry my mistake, i posted the following code earlier, this is tested
data want;
input address&$100.;
if address=prxchange('s/^(APT)(.*)/$2/i',-1,address);
cards;
APT 6 651 DALE ST
APT 2618 1214 OLD LASCASSAS RD
APT 305 8384 RIVER FORK
APT #D UNIVERSITY ST
;
This code keeps deleting my observation. I even copied and pasted exactly what you posted and in the log I get:
1098 data want;
1099 input address&$100.;
1100 if address=prxchange('s/^(APT)(.*)/$2/i',-1,address);
1101 cards;
NOTE: The data set WORK.WANT has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
1106 ;
1107 run;
If it matters I have SAS 9.4
address=left(tranwrd(address,'APT ',''));
nextword=scan(address,1);
address=substr(address,length(nextword)+2);
You are the best! This worked for me. I wish I had half your brain..
Also try regular expressions as below
if prxmatch('m/^APT.*/oi',address)=0;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.