For a sample data generated by the code below, I want to replace y1 to y10 variables with y_norm y_bad and y_good such that y_norm is repeated r_norm times, y_bad is repeated r_bad times and y_good is repeated r_good times . Instead of directly replacing, generating these variables with this rule is also fine.
For instance, for first row: y1, y2, y3 should take the value of y_norm because r_norm=3, then the remaining y4 to y10 variables should take the value of y_good because r_good = 7.
Similarly, for second row: variables y1 to y4 takes the values of y_norm (since r_norm=4) , y5 takes the value of r_bad (since r_bad=1), and y6 to y10 takes the value of y_good (since r_good=5).
How can this be done with STATA?
The code for sample data:
For instance, for first row: y1, y2, y3 should take the value of y_norm because r_norm=3, then the remaining y4 to y10 variables should take the value of y_good because r_good = 7.
Similarly, for second row: variables y1 to y4 takes the values of y_norm (since r_norm=4) , y5 takes the value of r_bad (since r_bad=1), and y6 to y10 takes the value of y_good (since r_good=5).
How can this be done with STATA?
The code for sample data:
Code:
clear all set obs 15 gen id = _n set seed 1198810031 gen y_norm = runiform(500, 12000) gen y_bad = y_norm/3 * runiform(0,1) gen y_good = y_norm*4 gen r_norm = runiformint(0,5) gen r_bad = int(3-r_norm) replace r_bad = r_bad*(-1) if r_bad <0 gen r_good = 10-r_norm - r_bad forvalues i = 1/10 { gen y`i' = . }
Comment