Announcement

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

  • Foreach loop

    I am attempting to create a variable (post_int) which tells me whether there are months of unemploment after the interview month. I am not going into too much detail (such as why), because it would just make this question really long. But please ask, if it is indeed relevant.

    This is an example where I am only interested in creating post_int for the year of 2004:
    ID Year temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8 temp9 temp10 temp11 temp12 Month of interview "pmonin"
    34 2003 . . 1 1 1 1 1 1 1 1 1 1 3
    34 2004 1 . . . . . . . . . . . 4
    The columns with the numbers 1-12 represent whether someone was unemployed in the previous year. So in this case the person was unemployed until January 2003 and interviewed in March 2003. post_int should therefore stay zero in 2004.

    However, the output puzzles me. It tells me s.th. about "temp0" and I cannot imagine where it got the zero from. Month of interview "pmonin" is going from 1 to 12 all missings are coded as "." or ".a".

    HTML Code:
    . sort id year
    . g post_int = 0
    . local n = !missing(L.pmonin)
    
    .
    . foreach i of varlist temp`n'-temp12{
      2.         replace post_int=1 if `i'==1
      3. }
    variable temp0 not found
    r(111);
    I am using Stata 11.
    Last edited by sladmin; 06 Feb 2018, 09:39. Reason: anonymize user

  • #2
    Go back to the line

    Code:
    local n = !missing(L,pmonin)
    The right-hand side

    Code:
    !missing(L,pmonin)
    would define an entire variable, but you've tried to put that result, a variable's worth of values, into a local macro.

    That is legal, but the consequence is only rarely what you want. What Stata does is just use the value in the first observation to put in the local macro.

    In the first observation you have necessarily have missing on L.pmonin. That follows immediately from the definition as the lag operator means we want to know the value before the first observation, and we don't know that: it's not in the dataset. So the lagged value is automatically missing, and the logical test for being not missing then returns 0 for false.

    That is precisely where Stata got the value for
    local n of 0 and as you do not have a variable temp0, you get thrown out.

    So much for that.

    I'll try to think up what you need for this bizarre-looking data structure. If someone else gets there first, splendid.

    EDIT: Sorry, but I don't understand this. Back to the day job.
    Last edited by Nick Cox; 05 May 2017, 07:46.

    Comment


    • #3
      Dear Nick,

      thank you very much for your response! It is great that you explained to me exactly why my code was not working. Because I did not needed to create this variable for all of my observations, but just for a particular subset of respondents (and not including the very first observation), I was able to work around it and now it works!

      Thank you very much!

      Comment

      Working...
      X