I have a dataset with 6 variables containing specific phrases. I have one phrase "Germ stat" I would like to code out to a new variable if this phrase exists in any of the 6 variables
For example, if variable 1 has the phrase "Germ stat" and variables 2 - 6 are empty, replace new_var to be 1 BUT if variable 1 has the phrase "Germ stat" and variable 2 has another phrase "Cross bred" or any of the other 5 has something else, do not replace new_var.
I am using the code below but it only codes out new_var if the phrase is in any of the 6 variables without regard for other phrases. How do I fix my code?
gen new_var=.
foreach var of varlist var1-var6 {
replace new_var=1 if ustrpos(`var', "Germ stat")
}
For example, if variable 1 has the phrase "Germ stat" and variables 2 - 6 are empty, replace new_var to be 1 BUT if variable 1 has the phrase "Germ stat" and variable 2 has another phrase "Cross bred" or any of the other 5 has something else, do not replace new_var.
I am using the code below but it only codes out new_var if the phrase is in any of the 6 variables without regard for other phrases. How do I fix my code?
gen new_var=.
foreach var of varlist var1-var6 {
replace new_var=1 if ustrpos(`var', "Germ stat")
}
Comment