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])
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)
Chris
0 Response to generating dummy variables for consecutive years
Post a Comment