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

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? 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
	address=left(tranwrd(address,'APT ','')); 
	nextword=scan(address,1);
	address=substr(address,length(nextword)+2);
--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26
if address=:'APT ' then delete;
--
Paige Miller
IsaacYi881
Calcite | Level 5

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;

 

PaigeMiller
Diamond | Level 26

@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);
--
Paige Miller
IsaacYi881
Calcite | Level 5

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

Jagadishkatam
Amethyst | Level 16

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
;
Thanks,
Jag
IsaacYi881
Calcite | Level 5

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

Jagadishkatam
Amethyst | Level 16

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
;

 

Thanks,
Jag
IsaacYi881
Calcite | Level 5

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

PaigeMiller
Diamond | Level 26
	address=left(tranwrd(address,'APT ','')); 
	nextword=scan(address,1);
	address=substr(address,length(nextword)+2);
--
Paige Miller
IsaacYi881
Calcite | Level 5

You are the best! This worked for me. I wish I had half your brain..

Jagadishkatam
Amethyst | Level 16

Also try regular expressions as below

 

if prxmatch('m/^APT.*/oi',address)=0;
Thanks,
Jag

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 2712 views
  • 1 like
  • 3 in conversation