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

Hello,

 

I  have the following dataset and want to replace the last word if it is 'MD' or 'M.D.' or 'MD.' with 'MD'.

 

DATA EXAMPLE1;
  INPUT Names $char30.;
DATALINES;
AARON RAY, M.D.
AARON,RAY MD.
RAY,AARON,M.D.
PMD RAY ARON M.D
AARON MD RAY 
AARON RAY
;
run;

I have used the following codes to address the different formats:

if scan(Names,-1,' ')=' MD. ' then Name_temp=tranwrd(Names,' MD. ',' MD ');
if scan(Names,-1,' ')=' M.D. ' then Name_temp=tranwrd(Names,' M.D. ',' MD ');
if scan(Names,-1,' ')='M.D.' then Name_temp=tranwrd(Names,'M.D.','MD');
if scan(Names,-1,' ')=',M.D.' then Name_temp=tranwrd(Names,',M.D.',' MD ');
if scan(Names,-1,' ')=' M.D.' then Name_temp=tranwrd(Names,' M.D.',' MD ');
if scan(Names,-1,' ')=' M.D ' then Name_temp=tranwrd(Names,' M.D ',' MD ');

 But only the first name is getting converted.

Let me know what I can do get this output

Obs Names                         Name_temp

1 AARON RAY, M.D.           AARON RAY, MD
2 AARON,RAY MD.            AARON RAY, MD
3 RAY,AARON,M.D.           AARON RAY, MD
4 PMD RAY ARON M.D     AARON RAY, MD
5 AARON MD RAY
6 AARON RAY

1 ACCEPTED SOLUTION

Accepted Solutions
Priyamvada07
Obsidian | Level 7
Works perfectly! Code provided by someone on another community.

data want; set example1; last_word = scan(names, -1, ', '); if(compress(last_word, '. ') = 'MD') then name_temp = tranwrd(names, strip(last_word), 'MD'); run;

View solution in original post

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Like this?

NEWNAME=prxchange('s/M\.?D.\.? *\Z/MD/,1,OLDNAME);

 

Priyamvada07
Obsidian | Level 7
Works perfectly! Code provided by someone on another community.

data want; set example1; last_word = scan(names, -1, ', '); if(compress(last_word, '. ') = 'MD') then name_temp = tranwrd(names, strip(last_word), 'MD'); run;
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
  • 2 replies
  • 1015 views
  • 1 like
  • 2 in conversation