我正在尝试从供应商名称中删除某些子字符串。所以我放入一个宏并执行此操作:
local item "BANKOFAMERICA" " INC" " INCORPORATED" " SYS " " SYSTEMS" " PVT" " PRIVATE" " LIMITED" " LTD" " LLC" " CORP" "LIMITED LIABILITY" "CORPORATION" " CORP " " COMPANY" " CO " " TECHNOLOGY" " TECH " " GLOBAL"
foreach v in `item' {
replace vendor = subinstr(vendor,"`v'","",.)
}
然而,这给我一个太少的引号错误。我尝试在“v”周围复合“”但这不起作用。知道我做错了什么/另一种方法来实现这个目标吗?
答案 0 :(得分:3)
您的嵌套引号给您带来麻烦。可以通过在本地宏定义中添加一些compound double quotes和替换:
来修复代码local item `" "BANKOFAMERICA" " INC" " INCORPORATED" " SYS " " SYSTEMS" " PVT" " PRIVATE" " LIMITED" " LTD" " LLC" " CORP" "LIMITED LIABILITY" "CORPORATION" " CORP " " COMPANY" " CO " " TECHNOLOGY" " TECH " " GLOBAL" "'
foreach v of local item {
replace vendor = subinstr(vendor,`"`v'"',"",.)
}
或者,您可以避免定义本地宏,只需使用:
foreach v in "BANKOFAMERICA" " INC" " INCORPORATED" " SYS " " SYSTEMS" " PVT" " PRIVATE" " LIMITED" " LTD" " LLC" " CORP" "LIMITED LIABILITY" "CORPORATION" " CORP " " COMPANY" " CO " " TECHNOLOGY" " TECH " " GLOBAL" {
replace vendor = subinstr(vendor,"`v'","",.)
}
这不像第一个版本那么清晰。
答案 1 :(得分:2)
@Dimitriy V.Masterov的答案很好。这只是阐述了一个关键细节。
您可以通过
查看错误. local item "BANKOFAMERICA" " INC" " INCORPORATED" " SYS " " SYSTEMS" " PVT" " PRIVATE" " LIMITED" " LTD" " LLC" " CORP" "LIMITED LIABILITY" "CORPORATION" " CORP " " COMPANY" " CO " " TECHNOLOGY" " TECH " " GLOBAL"
. mac li
第一个和最后一个"
被剥离为标记宏的开头和结尾的分隔符。你必须先使用复合双引号来阻止它。到目前为止Stata关注的是“和”是匹配的字符(可以嵌套,虽然这里不需要)。