Hi,
I am trying to generate a variable called "name" that is a simplification of my two variables str_name and str_name_other. Basically, name would take the values of str_name unless str_name has "-oth-" or "Other" as a value, in which case it would take the value of str_name_other.
I tried different ways of coding this but strangely the code doesn't yield the expected results i.e. nothing happens or the results are incomplete.
Here is what I tried :
Could anyone help me find what is wrong with my code?
EDIT : By the way, I suspect my dataset to have names containing the word "other" that are NOT to be replaced (a firm name containing the word "another", for instance). Could anyone give me a solution that is available for cells containing exclusively the keywords "Other", "other", "-oth-", etc. and nothing else?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str27 str_name str14 str_name_other "Other" "xxx" "-oth-" "yyyy" "zzz" "" end
I tried different ways of coding this but strangely the code doesn't yield the expected results i.e. nothing happens or the results are incomplete.
Here is what I tried :
Code:
gen name = "" replace name = subinstr(str_name, "-oth-", str_name_other, .) replace name = subinstr(str_name, "Other", str_name_other, .)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str27 str_name str14 str_name_other str5 name "Other" "xxx" "xxx" "-oth-" "yyyy" "-oth-" "zzz" "" "zzz" end
Code:
local other = strpos(str_name, "Other")
local other2 = strpos(str_name, "-oth-")
gen name = str_name
if `other' > 0 | `other2' > 0 {
replace name = str_name_other
}
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str27 str_name str14 str_name_other str5 name "Other" "xxx" "xxx" "-oth-" "yyyy" "yyyy" "zzz" "" "" end
EDIT : By the way, I suspect my dataset to have names containing the word "other" that are NOT to be replaced (a firm name containing the word "another", for instance). Could anyone give me a solution that is available for cells containing exclusively the keywords "Other", "other", "-oth-", etc. and nothing else?

Comment