首页 > 解决方案 > 将字符串中包含句点的缩写替换为 SAS 中没有句点的缩写

问题描述

我有以下数据集,如果它是“MD”或“MD”或“MD”,我想替换最后一个单词。与“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;
Data Convert;
Set Example1;
*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 ');
run;

但只有名字被转换。

让我知道我能做些什么来得到这个输出

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

标签: sassubstitution

解决方案


与其搜索所有可能的MD拼写方式,不如将最后一个单词标准化MD并搜索它。

data want;
    set example1;

    last_word = scan(names, -1, ', ');

    if(compress(last_word, '. ') = 'MD') 
        then name_temp = tranwrd(names, strip(last_word), 'MD');
run;

推荐阅读