Hello,
I am looking for some guidance.
I have 100 subjects with, 20 variables, 10 are labeled X1, X2, X3, X4 ... X10, and 10 are labeled Y1, Y2, Y3, Y4 ... Y10.
For each subject, I want to create another variable Z that is equal to either X1 or X2 or X3 ... or X10 chosen at random.
Variables X1...X10 and Y1...Y10 can have missing data, labeled . , therefore I only want to assign Z when the X and corresponding Y are not missing for that subject.
I also want to create a variable "ran" that tracks which random X that was chosen.
I've tried to solve this problem multiple ways but can't quite figure it out. I'm using Stata 13.
gen ran = .
gen Z = .
forvalues x = 0/5 {
local i = `x' + ceil(5 * uniform())
replace ran = `i' if Z == .
replace Z = X`i' if Z == . & X`i' != . & Y`i' != .
}
This loop doesn't work as it will pick a random X variable, assign it to all subjects that don't have missing values for X and Y, then will pick another random X variable and assign it to all the subjects that had missing values the first time around, and so on...
Thanks for your suggestions.
Best,
Richard
I am looking for some guidance.
I have 100 subjects with, 20 variables, 10 are labeled X1, X2, X3, X4 ... X10, and 10 are labeled Y1, Y2, Y3, Y4 ... Y10.
For each subject, I want to create another variable Z that is equal to either X1 or X2 or X3 ... or X10 chosen at random.
Variables X1...X10 and Y1...Y10 can have missing data, labeled . , therefore I only want to assign Z when the X and corresponding Y are not missing for that subject.
I also want to create a variable "ran" that tracks which random X that was chosen.
I've tried to solve this problem multiple ways but can't quite figure it out. I'm using Stata 13.
gen ran = .
gen Z = .
forvalues x = 0/5 {
local i = `x' + ceil(5 * uniform())
replace ran = `i' if Z == .
replace Z = X`i' if Z == . & X`i' != . & Y`i' != .
}
This loop doesn't work as it will pick a random X variable, assign it to all subjects that don't have missing values for X and Y, then will pick another random X variable and assign it to all the subjects that had missing values the first time around, and so on...
Thanks for your suggestions.
Best,
Richard
Comment