Announcement

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

  • generating dummy variables for consecutive years

    Hi everyone!

    I am quite new to Stata and I probably have a simple question, but I am not able to figure it out on my own.

    I have an unbalanced dataset with N individuals who have a unique identifier (id). I have several survey years (years) and an indicator (work) whether an individual had a paid job in the relevant year (yes=1, no=0).

    Now I would like to create several dummy variables (basically one dummy for every year) to see if the individual worked over several consecutive years, e.g. work1- work4.

    Meaning that if individual 1 worked in year 2001, dummy variable work1 should take the value 1. If individual 1 also worked one year later (2002), dummy variable work2 should also be equal to 1. If individual 1 did not work in year 2003, work1-work4 should equal 0 for this year.

    What I mean should look like this:
    id year work work1 work2 work3 work4
    1 2001 1 1 0 0 0
    1 2002 1 0 1 0 0
    1 2003 0 0 0 0 0
    2 2002 1 1 0 0 0
    2 2003 1 0 1 0 0
    2 2004 0 0 0 0 0
    3 2008 1 1 0 0 0
    3 2009 1 0 1 0 0
    3 2010 1 0 0 1 0
    4 2005 1 1 0 0 0
    4 2006 1 0 1 0 0
    4 2007 1 0 0 1 0
    4 2008 1 0 0 0 1

    I tried it with the following code, but unfortunately my idea does not work.

    sort persnr syear
    gen work1=1 if (work == 1 & work[_n-1] == 0 & id == id)
    replace work1=0 if missing(work1)

    gen work2=1 if (work == 1 & work[_n-1] == 1 & id == id[_n-1])
    replace work2=0 if missing(work2)
    Hope someone can help. Thanks a lot!
    Chris

  • #2
    Is there noone who can help me out?

    Comment


    • #3
      Diffidence in answering here arises, I guess, from difficulty in working out what you want and even whether it makes sense. At least, that was my reaction on first reading this.

      For example, work1 is 1 if and only each individual works in the first year of record and the next; but why is that fruitful -- because the years of record differ between individuals?

      Also, what in the example data licenses 1 for work2 for individual 1 in 2002 as they did not work in 2003?

      Also, what in the example data licenses 1 for work2 for individual 2 in 2003 as they did not work in 2004?

      Also, what in the example data licenses 1 for work3 for individual 3 in 2010 as there is no data for 2011?

      Also, what in the example data licenses 1 for work4 and individual 4 in 2008 as there is no data for 2009?

      My guess is here a small mess of small errors. Your definitions may however imply code like this

      Code:
      bysort id (year): gen length = _N
      
      su length, meanonly 
      local J = r(max) - 1 
      
      forval j = 1/`J'  
           by id: gen wanted`j' = work[`j'] == 1 & work[`j' + 1] == 1 
      }  :

      Comment


      • #4
        Hi Nick, thanks for you reply.

        I am working with an unbalanced panel, therefore I do not have observations for each Individuum for each year.
        E.g. work1 should refer to the first year the individual is observed. In the case of ID its the year 2001 and in the case of id 2 it is the year 2002.

        Unfortunately, you code does not produce what I am looking for
        Maybe I didn't express myself well. I mean the following:

        for id 1 in year 2001, work1 should be 1 (because work==1).
        for id 1 in year 2002, work1 should be 0 and work2 should be 1 (because work==1) (and work2 refers to the second observation of id 2)
        for id 1 in year 2003, work1-work4 should be 0 (because work==0).

        To answer your question:
        "What in the example data licenses 1 for work2 for individual 1 in 2002 as they did not work in 2003?"

        2003 does not matter because work2 refers to the second time the individual is observed (in my case 2002 and in 2002 the individual worked (work==1)).


        Thank you in advance!
        Last edited by Chris Weber; 08 Mar 2020, 08:49.

        Comment


        • #5
          I understand that you don’t have balance. That drives my first question, which I can’t see that you have addressed. Evidently I don’t understand what you want otherwise. Sorry, but bailing out.

          Comment

          Working...
          X