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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 586 views
  • 1 like
  • 2 in conversation