user1773010
user1773010

Reputation: 107

Stata substring error

I am trying to delete certain substrings from vendor names. So I put in a macro and do this:

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

However, this gives me a too few quotes error. I tried compounding the "" around `v' but that didn't work. Any idea what I am doing wrong/another way to accomplish this?

Upvotes: 1

Views: 719

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

The answer by @Dimitriy V. Masterov is fine. This just spells out a key detail.

You can see your error by going

. local item "BANKOFAMERICA"  " INC" " INCORPORATED" " SYS " " SYSTEMS" " PVT" " PRIVATE"    " LIMITED" " LTD" " LLC" " CORP" "LIMITED LIABILITY" "CORPORATION" " CORP " " COMPANY" " CO " " TECHNOLOGY" " TECH " " GLOBAL"

. mac li 

What happens is that the first and last " are stripped as delimiters marking the beginning and end of the macro. You must stop that by using compound double quotes first. So far as Stata is concerned `" and "' are matching characters (which can be nested, although that is not needed here).

Upvotes: 2

dimitriy
dimitriy

Reputation: 9460

Your nested quotes are giving you trouble. Your code can be fixed with the addition of some compound double quotes in the local macro definition and the replace:

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

Alternatively, you can avoid defining the local macro and just use:

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

This is not as legible as the first variant.

Upvotes: 3

Related Questions