Announcement

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

  • Nested foreach command

    Hello All,

    I would like to ask your help with my Stata problem. I am trying to do a nested foreach in a single Stata command, for example:

    foreach x in x1 x2 x3{
    foreach y in y1 y2 y3{
    gen dummy_`y'=(`x'==1&`y'==1)
    }
    }

    where: x1 x2 x3 are dummies =1 if they experienced economic shock in 2014 or 2015;
    y1 y2 y3 are dummies for worse-off condition (after experiencing these shocks, their household condition worsen)

    Essentially, what I aim to find are observations where they experienced economic shocks either in 2014 or 2015 (x1 | x2 | x3=1), and they became worse off (y1 | y2 | y3=1).

    When I ran the command above, it says that "variable dummy_y1 already defined"

    Any assistance greatly appreciated.

    Many thanks in advance,

    Christian

  • #2
    Welcome to Statalist, Christian.

    It looks to me as if you want a single indicator variable that is true if one of the x's is 1 and one of the y's is 1.

    If that is correct, you do not need a loop, much less two loops. Try
    Code:
    gen dummy = inlist(1,x1,x2,x3) & inlist(1,y1,y2,y3)
    If that is not correct, then the problem with your double loop is that the first time through the foreach x loop you generate dummy_y1 (and dummy_y2 and dummy_y3) and the second time through the foreach x loop you try to generate them again, when instead they need to be replaced.

    Without being sure what you were trying to accomplish with this code, the following code does the same thing, without the error you encountered, although since it is untested, it may have some other error.
    Code:
    foreach y in y1 y2 y3 {
        generate dummy_`y' = 0
        foreach x in x1 x2 x3 {
             replace dummy_`y' = 1 if `x'==1 & `y'==1
            }
        }

    Comment


    • #3
      Hi William,

      Thank you for your quick response. Basically, I want to create dummies =1 if x1=1 & y1=1, ..., xn=1 & yn=1, and not if any of the x's=1 and any of the y's=1. Both x1 and y1 should match (or should both be =1).

      What it means is that, if a person has experienced any type of shock in 2014 or 2015 (=1), for instance, x1=1 (where x1 pertains to loss of livestock due to theft) or x2=1 (where x2 is loss of agricultural crops due to flooding), and their condition worsened due to these shocks (y1=1 where =1 means worse off, while y2=0 where =0 means no change in household condition). So what I want to show in these newly created dummy variables are:

      dummy_y1=1 since x1=1 & y1=1
      dummy_y2=0 since x2=1 & y2=0
      etc.

      From these newly created dummies, I will create one new variable (worseoff=inlist(1,dummy_y1,dumm_y2,...) similar with your first code that identifies a person who experienced any shock in 2014/2015 and who became worse off because of these shocks.

      Apologies if I was not clear enough.

      Thank you in advance.

      Comment


      • #4
        Thanks for the clarification.

        I think this will do the trick.
        Code:
        generate dummy_y1 = x1==1 & y1==1
        generate dummy_y2 = x2==1 & y2==1
        generate dummy_y3 = x3==1 & y3==1
        Or, to avoid the intermediate dummy variables,
        Code:
        gen worseoff = inlist(1,x1==1&y1==1,x2==1&y2==1,x3==1&y3==1)
        More generally (for when you have 20 different pairs of variables instead of 3)
        Code:
        forvalues i=1/3 {
            generate dummy_y`i' = x`i'==1 & y`i'==1
            }
        Or, to avoid the intermediate dummy variables,
        Code:
        generate worseoff = 0
        forvalues i=1/3 {
            replace worseoff = 1 if x`i'==1 & y`i'==1
            }
        Hope one or more of these is right, it's late in my timezone.

        Comment


        • #5
          Thanks a lot William!! The first code worked.

          Have a good rest.

          Comment

          Working...
          X