Hi,
I have the next data
Id n_lo1 gr1 gr2 gr3 gr4 gr5 gr6 Pri
01 0600 15 30 54 78 96 45 A1
02 0700 20 0 12 41 52 25 A1
03 0800 30 40 12 14 23 63 A1
04 0900 45 12 10 12 45 45 A2
05 1000 2 52 8 78 12 67 A2
06 1100 14 20 69 11 14 21 A2
07 1200 78 5 74 78 78 58 A2
08 1300 5 2 90 32 98 47 A3
and I want to generate vars with a lot of combinations but for the example I just gonna write this two: 1) CDT_0600_F_1 2) CDT_0600_F_2
Id n_lo1 gr1 gr2 gr3 gr4 gr5 gr6 Pri CDT_0600_F_1 CDT_0600_F_2 .......
01 0600 15 30 54 78 96 45 A1 15 30
02 0700 20 0 12 41 52 25 A1 . .
03 0600 30 40 12 14 23 63 A1 30 40
03 0800 30 40 12 14 23 63 A1 . .
04 0900 45 12 10 12 45 45 A2 . .
05 1000 2 52 8 78 12 67 A2 . .
06 1100 14 20 69 11 14 21 A2 . .
07 1200 78 5 74 78 78 58 A2 . .
08 1300 5 2 90 32 98 47 A3 . .
For that reason, I created the next sintaxis
levelsof n_lo1,local(levels)
foreach m of local levels{
foreach j of numlist 1/6{
gen CDT_`m'_F_`j'=gr`j' if c_lo1=="`m'" & pri=="A1")
gen CDT_`m'_F_`j'=gr`j' if c_lo1=="`m'" & pri=="A2" & `j'<=4)
gen CDT_`m'_F_`j'=gr`j' if c_lo1=="`m'" & pri=="A3" & `j'<=2)
}
}
But It doesn't work with the conditions: ("pri=="A2" & `j'<=2") or (pri=="A3" & `j'<=2) or (pri=="A1")
because it still creating vars with all combinations in spite of the conditions, For example. I just want the next vars in case Pri="A3"
CDT_1300_F_1
CDT_1300_F_2
And it creates
CDT_1300_F_1
CDT_1300_F_2
CDT_1300_F_3
CDT_1300_F_4
CDT_1300_F_5
CDT_1300_F_6
I hope you can help me.
Kind Regards,
S.
Related Posts with Create vars with conditions inside a loop
Generate random GPS points within polygonsHello, I am trying to generate 50 random GPS points in each polygon of a shapefile that I have. I'v…
How to generate wealth share of the top 10% in wealth distribution using weighted data?Dear Listers, I have data which provides me with information on wealth of each household. ID is the…
Testing to see if the value of categorical variable x for string m contains the value of variable y for string m.Hello Statalist, Please forgive me if this is a stupid question; I am new to Stata. I am working wi…
how to add up length of stays in hospital for certain hospitalizationshi all, I ve been trying to perform this task for a week now with no luck. I have a data set (each …
HelpI have country-wise GDP data from 1997 to 2016 how do I generate a country-wise 5 years forward movi…
Subscribe to:
Post Comments (Atom)
0 Response to Create vars with conditions inside a loop
Post a Comment