I want to recode 4 variables the same way using loops. I followed the instructions of Scott Long on the Workflow of Data Analysis (pg 99)
Here is my code:
local likehert "prepquestn askquestn discquestn confiquestn" /*likehert is the name of the macro for my 4 variables*/
foreach varname of varlist 'likehert' {
generate t'varname' = 'varname' /* I want a new variable name that adds a 't' to the source variable (e.g tprepquestn from prepquestn )*/
replace t'varname' = 1 if 'varname' == 2 /*I want to replace values from the old variable into the new variable.*/
replace t'varname' = 2 if 'varname' == 4
replace t'varname' = 3 if 'varname' == 5 |'varname' == 6
replace t'varname' = 4 if 'varname' == 3
replace t'varname' = 5 if 'varname' == 1
}
The error message from STATA is ' invalid name.
Myself and a colleague have examined this closely, but I do not know where we are going wrong.
Thanks
Here is my code:
local likehert "prepquestn askquestn discquestn confiquestn" /*likehert is the name of the macro for my 4 variables*/
foreach varname of varlist 'likehert' {
generate t'varname' = 'varname' /* I want a new variable name that adds a 't' to the source variable (e.g tprepquestn from prepquestn )*/
replace t'varname' = 1 if 'varname' == 2 /*I want to replace values from the old variable into the new variable.*/
replace t'varname' = 2 if 'varname' == 4
replace t'varname' = 3 if 'varname' == 5 |'varname' == 6
replace t'varname' = 4 if 'varname' == 3
replace t'varname' = 5 if 'varname' == 1
}
The error message from STATA is ' invalid name.
Myself and a colleague have examined this closely, but I do not know where we are going wrong.
Thanks
Comment