I believe this is a common problem but I have not found an answer online. I have a large dataset with multiple numeric binary variables with "no" response options coded as 2 instead of 0. Additionally, there are no value labels in the dataset. Fortunately, when a variable has values equal to either 1 or 2, it is always a yes/no question. Therefore, I would like to create a loop that changes the value 2 to 0 only for binary variables with response options equal to 1 or 2 (or have a max value of 2). I would also like to create value labels. I provided the code I have so far and an example dataset below. In the example dataset, all the variables are binary except for 'numkids', so I would not want to change the values of that variable. Could you help me fix the blue part of my code?
Thank you,
Tom
local imputedvars *_i
foreach var of `imputedvars’ {
recode `var' (2=0) if `var' has values equal to 1, 2, or .
label define yesno 0 “No” 1 “Yes”
label values `var’ yesno if `var' has values equal to 0, 1, or .
}
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input byte(id male white older21 numkids married) 1 1 2 1 0 1 2 2 1 1 3 2 3 1 2 2 2 1 4 2 2 2 0 1 5 1 1 2 1 2 6 2 2 1 1 1 7 1 1 2 2 2 8 2 1 1 0 1 end
0 Response to Changing values of yes/no binary variables from 1/2 to 1/0 using loop
Post a Comment