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