Hi,
I am trying to simplify my code to generate and replace multiple variables. Below is the code that I started with:
generate batch5=1 if (bTChol<143.66 & batch==33)
replace batch5=2 if (bTChol<143.66 & batch==34)
replace batch5=3 if (bTChol<143.66 & batch==35)
replace batch5=4 if (bTChol<143.66 & batch==36)
replace batch5=5 if (bTChol<143.66 & batch==37)
replace batch5=6 if (bTChol<143.66 & batch==38)
replace batch5=7 if (bTChol<143.66 & batch==39)
replace batch5=8 if (bTChol<143.66 & batch==40)
generate batch6=1 if (bTChol<143.66 & batch==41)
replace batch6=2 if (bTChol<143.66 & batch==42)
replace batch6=3 if (bTChol<143.66 & batch==43)
replace batch6=4 if (bTChol<143.66 & batch==44)
replace batch6=5 if (bTChol<143.66 & batch==45)
replace batch6=6 if (bTChol<143.66 & batch==46)
replace batch6=7 if (bTChol<143.66 & batch==47)
replace batch6=8 if (bTChol<143.66 & batch==48)
And then I tried to use the -foreach command to simplify it because I have to generate 15 batches (batch1-batch15) with the condition of running the batch==1 to batch==126.
generate batch5=1 if (bTChol<143.66 & batch==33)
foreach i in 2 3 4 5 6 7 8{
foreach j in 34 35 36 37 38 39 40 {
replace batch5=`i' if (bTChol<143.66 & batch==`j')
}
}
But it did't give me the results that I wanted when I check it with the -tab command. It kinda collapse all the categories into one?
Is there a better solution to this?
Many thanks,
Wei Yee
Related Posts with Help with -foreach loop command
IVProbit postestimation testI am using MLE to estimate my IVProbit, which involves estimating a simultaneous system of equations…
nlsur with linear functions is giving me different results from sureg, Is this a bug?Good afternoon, When I estimate linear seemingly unrelated regression through the nlsur, I expect t…
Advice on coding for crude and standardised incidence rate estimationI recently completed analysis of a large dataset to generate age and sex stratified incident counts …
Percentage change of daily exchange rate dataHello everyone, I am writing my dissertation thesis on the impact of Brexit negotiations on the val…
2SLS InterpretationHello everyone, I hope you are all well, I have an analysis based on the convergence growth model …
Subscribe to:
Post Comments (Atom)
0 Response to Help with -foreach loop command
Post a Comment