I apologize in advance if this has been addressed in earlier forums.. I was unable to find it but may have been searching the wrong keywords.
My data looks like this:
patid .... var_code1 var_code2 .. var_code50
1________eye01____ear05______gas03
2________gas05____gas03______eye02
3________ear01_____ear02______ .
..... with about 30 different prefixes and suffixes of 04-20.
I am using Stata 13.1 on Windows and I would like to make the data look like this:
patid .... abc_eye01 abc_eye02 .. abc_eye20 abc_ear01-abc_ear15 abc_gas01-abc_gas20 ...
1 _________ 1 ______ 0 ______ 0 ....
2 _________ 0 ______ 1 ______ 0 ....
...
What I think I need to do is:
local abc "??"
foreach j of local abc {
gen abc_`j'=0
foreach i of varlist var_code1-var_code50{
replace abc_`j'=1 if `i'=="`j'"
}
}
But I don't know how to put in the list of new variables-to-be into the macro..
I tried local abc "eye01-eye20 ear01-ear15 gas01-gas20"
but this just created three entries of eye01-eye20 ear01-ear15 gas01-gas20, instead of eye01, eye02, eye03... etc.
I found some useful information about using unab or ds but since I don't have these variables in the set yet, I couldn't understand how to use those commands.
Could someone help me to figure out how to write the macro command?
Or am I approaching this incorrectly and should be doing another way?
Thank you for taking the time to read all this!
My data looks like this:
patid .... var_code1 var_code2 .. var_code50
1________eye01____ear05______gas03
2________gas05____gas03______eye02
3________ear01_____ear02______ .
..... with about 30 different prefixes and suffixes of 04-20.
I am using Stata 13.1 on Windows and I would like to make the data look like this:
patid .... abc_eye01 abc_eye02 .. abc_eye20 abc_ear01-abc_ear15 abc_gas01-abc_gas20 ...
1 _________ 1 ______ 0 ______ 0 ....
2 _________ 0 ______ 1 ______ 0 ....
...
What I think I need to do is:
local abc "??"
foreach j of local abc {
gen abc_`j'=0
foreach i of varlist var_code1-var_code50{
replace abc_`j'=1 if `i'=="`j'"
}
}
But I don't know how to put in the list of new variables-to-be into the macro..
I tried local abc "eye01-eye20 ear01-ear15 gas01-gas20"
but this just created three entries of eye01-eye20 ear01-ear15 gas01-gas20, instead of eye01, eye02, eye03... etc.
I found some useful information about using unab or ds but since I don't have these variables in the set yet, I couldn't understand how to use those commands.
Could someone help me to figure out how to write the macro command?
Or am I approaching this incorrectly and should be doing another way?
Thank you for taking the time to read all this!
Comment