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
Extracting information out of matrices' observations.Hey, I have a dataset of around 50 vars and 30k obsevations. I have created a matrix that stores re…
how to download -oparallel- package from SSCHello, I am trying to download the -oparallel- package manually so that I can copy/paste the packag…
event study linear regression modelDear all, I read previous posts about event study, but I still cannot find my starting point for co…
Using Collapse with Mi xeqI have a panel dataset organised on the individual level and I am trying to impute a categorical var…
How to find "plus" folder on MacHello, I am trying to find where my Mac saves new packages that I download from Stata. I tried typ…
Subscribe to:
Post Comments (Atom)
0 Response to Help with -foreach loop command
Post a Comment