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
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;
Like this?
NEWNAME=prxchange('s/M\.?D.\.? *\Z/MD/,1,OLDNAME);
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.