DATA Step, Macro, Functions and more

Proc Tranwrd replacing a single letter

Reply
Contributor
Posts: 55

Proc Tranwrd replacing a single letter

Hi everyone,

 

I'm working on an address data set I have several addresses that have a "E" in place of "East" for example:

 

Instead of "123 E Eggers Street" I want 123 "East Eggers Street"

 

But when i use 

 

New_address2 = tranwrd(New_address1,'E','EAST')

 

Every E is replaced with EAST. Is there anyway around this?

 

Thank you!

Frequent Contributor
Posts: 93

Re: Proc Tranwrd replacing a single letter

Add spaces before and after your search term?

 

New_address2 = tranwrd(New_address1,' E ','EAST')

Contributor
Posts: 55

Re: Proc Tranwrd replacing a single letter

Posted in reply to thomp7050
Hi Thomp, thanks for the suggestion! I tried a couple of variations space before and after, after only, before only and EAST is still appearing at unexpected places. Any other thoughts?
Frequent Contributor
Posts: 93

Re: Proc Tranwrd replacing a single letter

This code should work for you.  Does it not?

 

DATA MYDATA;
New_address2 = tranwrd("123 E Eggers Street",' E ',' EAST ');
RUN;

 

 

Contributor
Posts: 55

Re: Proc Tranwrd replacing a single letter

Posted in reply to thomp7050
It does work except, I have other entries in the data set and what happens is Avenue becomes avenueast
Frequent Contributor
Posts: 93

Re: Proc Tranwrd replacing a single letter

DATA MYDATA;
New_address2 = tranwrd("123 E Eggers Avenue",' E ',' EAST ');
RUN;

 

Produces "123 EAST Eggers Avenue", which I think is what you had hoped for (?)  Was there an example that you tried using this process and printed EAST unexpectedly?

Super User
Posts: 19,791

Re: Proc Tranwrd replacing a single letter

You need to post a larger sample data set and identify which records are being incorrectly converted. 

Include the code you're using as well and the log. 

 

Super User
Posts: 11,343

Re: Proc Tranwrd replacing a single letter

The Tranwrd function is working as designed: (from online help) Replaces all occurrences of a substring in a character string. 

 

You might look into FINDW to find the E as a word and then use other functions to insert the text.

One way:

data example;
   New_Address1 = "123 E Eggers Street";
   pos=findw(New_address1,'E');
   if pos>0 then New_address2 = catx(' ',substr(New_address1,1,Pos-1),"East",substr(New_address1,Pos+1));
run;

Note that will only change the first E

 

Super User
Posts: 10,028

Re: Proc Tranwrd replacing a single letter

data example;
   New_Address1 = "123 E Eggers Street";
   New_address2 = prxchange('s/\bE\b/East/i',-1,New_address1);
run;
Ask a Question
Discussion stats
  • 8 replies
  • 107 views
  • 0 likes
  • 5 in conversation