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

HI,


I have a variable for names and some of those names have some keywords I would like to take out.  I was trying using TRANWRD and it works, but I have to do so many TRANWRDS statements to eliminate all the possible keywords I have in my list.   I was trying to do a loop, but I am having problems.  Can anyone help?

Example Data:

IDORIGINAL_NAME
1Jose L Smith JR
2Angel P Jones SR
3Susan DECD White III Trust
4Henry West IV
5John Adams
6Jose DECD Hernandez


Words I want to eliminate: 'JR', 'SR', 'III', 'IV', 'DECD'.


This is the last code I tried:


DATA WORK.NEWTABLE (KEEP=ID ORIGINAL_NAME NEW_NAME);

SET LIBRARY.NAMES_ID;

NEW_NAME=ORIGINAL_NAME;

ARRAY KEYWORDS{*} $ _TEMPORARY_ ('JR', 'SR', 'III', 'IV', 'DECD');

DO i=1 TO 5;

     NEW_NAME{i+1}=TRANWRD(NEW_NAME{ i }, KEYWORDS{ i }, '');

     END;

RUN;

Desired Results

IDORIGINAL_NAMENEW_NAME
1Jose L Smith JRJose L Smitn
2Angel P Jones SRAngel P Jones
3Susan DECD White III TrustSusan White Trust
4Henry West IVHenry West
5John AdamsJohn Adams
6Jose DECD HernandezJose Hernandez
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You don't have to make an array since you can just specify the list in the DO loop.

data want;

   set have;

   n_name = o_name;

   length word $27 ;

   do word='JR', 'SR', 'III', 'IV', 'DECD' ;

      n_name = tranwrd(' '||n_name, ' '||strip(word)||' ', ' ');

  end;

   n_name = compbl(n_name);

   drop word ;

run;

View solution in original post

6 REPLIES 6
ballardw
Super User

I'll bet that you had an array reference error.

New_name is not an array so

NEW_NAME=TRANWRD(NEW_NAME, KEYWORDS{ i }, '');

would take 5 passes through the step.

You might want to use

Do I = 1 to dim(Keywords); If you have to add another item, such as 'II', to your list you don't have to change the 5 to 6.

KachiM
Rhodochrosite | Level 12

Hope this answers you.

data have;

input id o_name $27.;

datalines;

1  Jose L Smith JR      

2 Angel P Jones SR      

3 Susan DECD White III Trust

4 Henry West IV         

5 John Adams            

6 Jose DECD Hernandez

;

run;

data want;

length n_name $27;

array k[5] $4 _temporary_ ('JR', 'SR', 'III', 'IV', 'DECD');

   set have;

   n_name = o_name;

   do i = 1 to dim(k);

      word = k;

      n_name = tranwrd(n_name, compress(word), '');

   end;

   n_name = compbl(n_name);

   output;

keep n_name;

run;

Tom
Super User Tom
Super User

You don't have to make an array since you can just specify the list in the DO loop.

data want;

   set have;

   n_name = o_name;

   length word $27 ;

   do word='JR', 'SR', 'III', 'IV', 'DECD' ;

      n_name = tranwrd(' '||n_name, ' '||strip(word)||' ', ' ');

  end;

   n_name = compbl(n_name);

   drop word ;

run;

Ksharp
Super User

You just want to get rid of them all ? That is it ?

data have;
input id o_name $27.;
datalines;
1  Jose L Smith JR      
2 Angel P Jones SR      
3 Susan DECD White III Trust
4 Henry West IV         
5 John Adams            
6 Jose DECD Hernandez
;
run;
data want;
 set have;
 new_name=prxchange('s/\s+(JR|SR|III|IV|DECD)\s+/ /o',-1,o_name);
run;

Xia Keshan

Patrick
Opal | Level 21

I would use '\b' instead of '\s' so the unwanted words can also be at the beginning or the very end of a string.

new_name=prxchange('s/\b(JR|SR|III|IV|DECD)\b/ /o',-1,o_name)

Ksharp
Super User

Yes. You are right. I overlook it  .

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 37759 views
  • 4 likes
  • 6 in conversation