BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

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!

8 REPLIES 8
thomp7050
Pyrite | Level 9

Add spaces before and after your search term?

 

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

hwangnyc
Quartz | Level 8
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?
thomp7050
Pyrite | Level 9

This code should work for you.  Does it not?

 

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

 

 

hwangnyc
Quartz | Level 8
It does work except, I have other entries in the data set and what happens is Avenue becomes avenueast
thomp7050
Pyrite | Level 9

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?

Reeza
Super User

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. 

 

ballardw
Super User

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

 

Ksharp
Super User
data example;
   New_Address1 = "123 E Eggers Street";
   New_address2 = prxchange('s/\bE\b/East/i',-1,New_address1);
run;

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
  • 8 replies
  • 877 views
  • 0 likes
  • 5 in conversation