1

我正在尝试从供应商名称中删除某些子字符串。所以我放了一个宏并这样做:

 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”周围复合“”,但没有奏效。知道我做错了什么/另一种方法来实现这一点吗?

4

2 回答 2

3

您的嵌套引号给您带来麻烦。您的代码可以通过在本地宏定义中添加一些复合双引号和替换来修复:

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'","",.)
}

这不像第一个变体那样清晰。

于 2013-08-05T22:08:26.433 回答
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 而言,“”和“”是匹配字符(可以嵌套,尽管这里不需要)。

于 2013-08-05T22:18:24.660 回答