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

Hi Guys, 

 

I'm creating an address file which I then need to format into columns based on number of characters. However, I am first trying to replace words within the address string to common abbreviations to cut down some of the length.

 

I have an address string on a file (work.addresses) like so:

 

123 maple court avenue, anycity, anystate

 

I have a separate list (work.abbrev) which gives me the abbreviations, it has 2 columns Word and Abbreviation:

 

Court | Ct

Avenue | Av

 

I want to know how I can do a tranwrd loop or something similar which will look at the words from the address string against the words on the abbreviations list and on the ones that match replace the matching word with the abbreviation. 

 

final result would be: 123 maple Ct Av, anycity, anystate

 

I'm not quite sure how to build the syntax..there was a similar topic on here however, it didn't really cover how to loop through a separate list of words to match

 

Any help would be much appreciated!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
nehalsanghvi
Pyrite | Level 9

Here you go:

data addresses;
Address='123 maple court avenue, anycity, anystate';
run;

data abbrev;
input Word$ Abbrev$;
datalines;
Court Ct
Avenue Av
;
run;

proc sql;
select count(*) into :abbrevcnt trimmed
from abbrev;

select cats(upcase(word),","), cats(upcase(abbrev),",") into :word1-:word&abbrevcnt., :abbrev1-:abbrev&abbrevcnt.
from abbrev;
quit;

%macro findreplace;
data addresses2;
set addresses;
%do z=1 %to &abbrevcnt.;
	Address=tranwrd(upcase(Address),"&&word&z.","&&abbrev&z.");
%end;
run;
%mend;

%findreplace;

 

To prevent the 'Court' in the name 'Maple Court' from being replaced with Ct, the code above looks for a comma to be present right after the word to be replaced.

View solution in original post

3 REPLIES 3
Reeza
Super User

Search Lexjansen.com for some pre-written macro's for address cleaning and standardizing. 

 

nehalsanghvi
Pyrite | Level 9

Here you go:

data addresses;
Address='123 maple court avenue, anycity, anystate';
run;

data abbrev;
input Word$ Abbrev$;
datalines;
Court Ct
Avenue Av
;
run;

proc sql;
select count(*) into :abbrevcnt trimmed
from abbrev;

select cats(upcase(word),","), cats(upcase(abbrev),",") into :word1-:word&abbrevcnt., :abbrev1-:abbrev&abbrevcnt.
from abbrev;
quit;

%macro findreplace;
data addresses2;
set addresses;
%do z=1 %to &abbrevcnt.;
	Address=tranwrd(upcase(Address),"&&word&z.","&&abbrev&z.");
%end;
run;
%mend;

%findreplace;

 

To prevent the 'Court' in the name 'Maple Court' from being replaced with Ct, the code above looks for a comma to be present right after the word to be replaced.

Josers18
Obsidian | Level 7
Thanks! this seemed to have done the trick!

sas-innovate-2024.png

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.

 

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
  • 3 replies
  • 1293 views
  • 4 likes
  • 3 in conversation