Hi,
I'm having a problem writing the correct loop code using an if condition.
I'm using Stata 14.1 for Windows.
I have set locals for each drug name & a local combining all the drug names. I have then generated a new variable for each of the drugs as follows:
local drug1 `" possible string names for drug "'
local drug2 `" possible string names for drug "'
local drug3 `" possible string names for drug "'
local alldrugs "drug1 drug2 drug3"
foreach var of local alldrugs {
gen `var'=.
}
I now want to replace the value of each new variable (drug1-drug3) with 1 if another variable (comdescription) contains the specific strings contained in the locals drug1-drug3 (i.e. replace the value of drug1 with 1 if comdescription matches any string held in the local drug1 and replace drug2=1 if string in comdescription matches any in drug2 local, etc.).
So far I have written the following code, but it is not correct & I am unsure about how to write the if condition. I want the if statement to say "if the name of the variable equals the name of the local".
foreach local of local alldrugs {
foreach varname of local `local' {
foreach x of local `local' {
if `varname'[1]==`local'[1] { // if condition not correct (if the name of the variable equals the name of the local
replace `varname'=1 if strmatch(lower(comdescription),"*`x'*")
}
}
}
}
After a number of real changes have been made, the error I get is that one of my strings held in drug1 is not found.
I could do this individually without the loops but I would like to learn how to use loops effectively & would really appreciate any advice on this matter.
Thank you for your time.
Best wishes,
Bryony
I'm having a problem writing the correct loop code using an if condition.
I'm using Stata 14.1 for Windows.
I have set locals for each drug name & a local combining all the drug names. I have then generated a new variable for each of the drugs as follows:
local drug1 `" possible string names for drug "'
local drug2 `" possible string names for drug "'
local drug3 `" possible string names for drug "'
local alldrugs "drug1 drug2 drug3"
foreach var of local alldrugs {
gen `var'=.
}
I now want to replace the value of each new variable (drug1-drug3) with 1 if another variable (comdescription) contains the specific strings contained in the locals drug1-drug3 (i.e. replace the value of drug1 with 1 if comdescription matches any string held in the local drug1 and replace drug2=1 if string in comdescription matches any in drug2 local, etc.).
So far I have written the following code, but it is not correct & I am unsure about how to write the if condition. I want the if statement to say "if the name of the variable equals the name of the local".
foreach local of local alldrugs {
foreach varname of local `local' {
foreach x of local `local' {
if `varname'[1]==`local'[1] { // if condition not correct (if the name of the variable equals the name of the local
replace `varname'=1 if strmatch(lower(comdescription),"*`x'*")
}
}
}
}
After a number of real changes have been made, the error I get is that one of my strings held in drug1 is not found.
I could do this individually without the loops but I would like to learn how to use loops effectively & would really appreciate any advice on this matter.
Thank you for your time.
Best wishes,
Bryony
Comment