我正在尝试使用subinstr()
命令删除某些名称中的连字符。不幸的是,没有删除单个连字符和以连字符开头的名称。 (有两个原因:" -
某些东西"更具体而不是"某些东西",我不想意外删除重要部分一个名字。)
为了取消连字符,我还需要添加额外的东西吗?
代码示例(完整代码在此处添加时间太长)
append using "2011 Death", force;
append using "2012 Death", force;
duplicates drop;
replace ID = subinstr(ID,"HLTH","HEALTH",.);
replace ID = subinstr(ID,"CORPORATION","",.);
replace ID = subinstr(ID,"ASSOCIATIONINC","",.);
replace ID = subinstr(ID,"-P&MED","PITALANDMED",.);
replace ID = subinstr(ID,"-HIGHLANDSMEDICALCENTER","",.);
replace ID = subinstr(ID,"-","",.);
(more along similar lines)
"-highlandmedicalcenter"
,"-P&MED"
和"-"
未被删除,因为处理后仍存在-highlandsmedicalcenter
内容的名称。前三个是。我不确定-p$med
。
答案 0 :(得分:1)
以下使用您的代码的示例适合我。我对您的经验的唯一解释是,您的数据可能包含短划线或短划线而非连字符。
. input str30 s1
s1
1. "HLTH"
2. "CORPORATION"
3. "ASSOCIATIONINC"
4. "-P&MED"
5. "-HIGHLANDSMEDICALCENTER"
6. "-GNXL"
7. end
. clonevar s2 = s1
. replace s2 = subinstr(s2,"HLTH","HEALTH",.)
(1 real change made)
. replace s2 = subinstr(s2,"CORPORATION","",.)
(1 real change made)
. replace s2 = subinstr(s2,"ASSOCIATIONINC","",.)
(1 real change made)
. replace s2 = subinstr(s2,"-P&MED","PITALANDMED",.)
(1 real change made)
. replace s2 = subinstr(s2,"-HIGHLANDSMEDICALCENTER","",.)
(1 real change made)
. replace s2 = subinstr(s2,"-","",.)
(1 real change made)
. list, clean
s1 s2
1. HLTH HEALTH
2. CORPORATION
3. ASSOCIATIONINC
4. -P&MED PITALANDMED
5. -HIGHLANDSMEDICALCENTER
6. -GNXL GNXL
.