Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Create vars with conditions inside a loop

    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 0800 30 40 12 14 23 63 A1 30 40
    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 n_lo1=="`m'" & pri=="A1")
    gen CDT_`m'_F_`j'=gr`j' if n_lo1=="`m'" & pri=="A2" & `j'<=4)
    gen CDT_`m'_F_`j'=gr`j' if n_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.
    Last edited by Sumiko EC; 13 Mar 2019, 18:15.
Working...
X