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
- How can I extract a part of a string variable which contains zipcode information?Hello, I'm working with Massachusetts zip code data and there are 3 kinds of formats the data is cur…
- Margins after menbreg with exposureHello, I am estimating the following multilevel negative binomial model with an exposure term. Co…
- calculate the difference between a date n and the first date of a long setPlease I need to calculate the difference between a date n and the first date of a long set. I do no…
- stset for multiple failure data queryDear all, I would like to ask for some advice on 'stset'-ing my dataset with multiple failures per …
- Calculate daily stock return relative to peak in the last 6 month periodI have daily stock market data and would like to calculate the return for each observation relative …
Subscribe to:
Post Comments (Atom)
0 Response to Help with -foreach loop command
Post a Comment