Looking for some advice regarding replacing/recoding variable dependent on responses in other variables.
The platform we use for data collection is ODK. For one question regarding side effects we have a 'select multiple' option on the database where the options are coded 0-13 (0=rash, 1=itchiniess, etc). When .csv data gets exported to stata the column 'sideeffects_initial' reads 0 2 4 5 10 or 1 11 12 for example which stata reads as string because of the spaces. Previously I'd been able to manage this problem with the code
replace itchiness = regexm(sideeffects_initial, "1")
but because this time I have more than 9 options this also recodes 10/11/12/13 as being positive for itchiness. I tried to search for help with this problem and in the end I've managed to split the variables into columns using:
split sideeffects_initial, parse(" ")
Then because I want to use wildcard of sideeffects_initial* I just renamed the initial variable
rename sideeffects_initial_o sideeffects_ini_o
I am then able to destring the newly created variables with
destring sideeffects_initial*, replace
I then want to create a new variable for each side effect (example itchiness, where it was originally coded 1) where if the number in any sideeffects_initial* column is equal to the side effect number. This is where I am having trouble.
I have tried using
egen itchinessss==1 if sideeffects_initial*==1
Which I believe didn't work as egen can't be used with wildcards.
I have then been trying to work out if it is possible to create a loop using wildcards.
I know it is possible to type out each individual variable name in a loop and do this but trying to avoid this as we have this question across multiple datasets and so would have to code each individually based on the number of sideeffects_initial(1-13) that is generated depending on the data inputted.
I came across the 'unab' function which I though may help - but still not working (error code: vars not found) based on below code.
unab vars : sideeffects_initial*
local varlist "vars"
foreach x in `varlist' {
replace itchiness=1 if `varlist'==1
}
Any advice greatly appreciated.
The platform we use for data collection is ODK. For one question regarding side effects we have a 'select multiple' option on the database where the options are coded 0-13 (0=rash, 1=itchiniess, etc). When .csv data gets exported to stata the column 'sideeffects_initial' reads 0 2 4 5 10 or 1 11 12 for example which stata reads as string because of the spaces. Previously I'd been able to manage this problem with the code
replace itchiness = regexm(sideeffects_initial, "1")
but because this time I have more than 9 options this also recodes 10/11/12/13 as being positive for itchiness. I tried to search for help with this problem and in the end I've managed to split the variables into columns using:
split sideeffects_initial, parse(" ")
Then because I want to use wildcard of sideeffects_initial* I just renamed the initial variable
rename sideeffects_initial_o sideeffects_ini_o
I am then able to destring the newly created variables with
destring sideeffects_initial*, replace
I then want to create a new variable for each side effect (example itchiness, where it was originally coded 1) where if the number in any sideeffects_initial* column is equal to the side effect number. This is where I am having trouble.
I have tried using
egen itchinessss==1 if sideeffects_initial*==1
Which I believe didn't work as egen can't be used with wildcards.
I have then been trying to work out if it is possible to create a loop using wildcards.
I know it is possible to type out each individual variable name in a loop and do this but trying to avoid this as we have this question across multiple datasets and so would have to code each individually based on the number of sideeffects_initial(1-13) that is generated depending on the data inputted.
I came across the 'unab' function which I though may help - but still not working (error code: vars not found) based on below code.
unab vars : sideeffects_initial*
local varlist "vars"
foreach x in `varlist' {
replace itchiness=1 if `varlist'==1
}
Any advice greatly appreciated.
Comment