SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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