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
Interpolate (extrapolate) values (mipolate)Hi Everyone, As my data set has some missing values, I have used the "mipolate" command found in st…
Construct the standard error for the SCAR (event study)Dear Stata community, I'm trying to replicate the empirical strategy of the paper from Aybar and Ay…
Industry and Country effect cross sectional regressionHello Statalist Forum, I am looking at the industry and country effect in 16 countries and 10 indus…
Non-linear constraint in a SURE estimationDear Statalist contributors, I am trying to estimate a system of seemingly unrelated labor supplies…
Pesaran CSD Test Keeps Giving Errors!Hi everyone, I use Stata13 and I have a panel data of 38 countries across 6 years. I tried performin…
Subscribe to:
Post Comments (Atom)
0 Response to Help with -foreach loop command
Post a Comment