I have a list of keywords, including both single words and compound words/phrases. I want to search a couple of variables to determine which observations include any of my keywords. The observations that have any of the key words I will keep, those with none of the keywords I will drop. I am building the basic code using Stata's auto dataset. I am using a macro to store the list of keywords, and then using word count and a while loop to sort out my observations. My problem is that I can't get the loops to complete over all the keywords, though I think it's set up properly. I suspect I am not using double quotes exactly right, but don't know if that is my only problem. Here is my code:
version 10
sysuse auto
gen dummy=0
gen string="AMC"
local kw Buick Ford "Audi Fox" Impala
local N: word count `"`kw'"'
local i=1
while `i'<=`N' {
local n: word `i' of `kw'
gen X1=regexm(make,`"`n'"')
gen X2=regexm(string,`"`n'"')
replace dummy=1 if X1==1 | X2==1
drop X1 X2
local i =`i'+1
}
drop if dummy==0
version 10
sysuse auto
gen dummy=0
gen string="AMC"
local kw Buick Ford "Audi Fox" Impala
local N: word count `"`kw'"'
local i=1
while `i'<=`N' {
local n: word `i' of `kw'
gen X1=regexm(make,`"`n'"')
gen X2=regexm(string,`"`n'"')
replace dummy=1 if X1==1 | X2==1
drop X1 X2
local i =`i'+1
}
drop if dummy==0
Comment