Dear Statalist,
I am doing an analysis, where I would like to control for the length of the last unemployment spell. The analysis looks at reemployed respondents with regard to a dichotomous dependent variable (here: "dep1"). I am working on this problem for a while now, but I cannot find a definite solution and I am grateful for any ideas on how to approach this.
The problem is that I need to attribute the spell length to the dependent variable. I think an illlustration of my dataset will show why this is difficult:
This is certainly a worst-case scenario in my data, but many of these cases exists and I am trying to account for such cases in my approach. Down below is the -dataex- version of the whole observation period of the person.
So, here is an example: For this person I have to times where he got reemployed in the table above and got interviewed (and therefore a value for the dependent value). But as you can see, he has multiple unemployment spells. "Temp1"-"Temp12" are the unemployment months (1=unemployed) in the previous year! Month of interview corresponds to the month (eg 3=March). My dependent variable is in the -dataex- below "dep1".
The problem: I can get the length of each spell reshaping to long-format to person-year-month and using -tsspell- counting the length of each spell. But I then would need to tell Stata to which year (where the dependent variable is) the spell length belongs. And since people like to one above often have multiple unemployment spells in a year; I can only think of one approach to create the length variable and attribute it to the corresponding year in which my dependent variable was asked.
But first let me show you how, in the end, it should look:
This is the only approach I can think of: Stata needs to count backwards. Go to the month of interview and start counting backwards, then attribute the total count to the row from which the month of the interview was taken.
This is where I stumble:
1) I cannot really think of a way to start the loop. Because a -while- loop seems to not work, because there is no ongoing criteria I could set -while- to TRUE. Like, e.g. I cannot say "while temp`n' == 1", because the loop starts often with "temp`n' == -2". But I need a way to tell the loop that I should start at the month of interview and then count until "-2" appears again.
2) Can Stata count over rows? So once it finishes e.g. at "temp1", it changes to "L.temp12"?
Really, I am grateful for every input, because I am slowly a little bit lost whether I might just not be able to control for spell length. If you think, btw, that this problem is much easier to handle in another program like R, please let me know, because I would then switch there.
Thank you very much!
-----
I am doing an analysis, where I would like to control for the length of the last unemployment spell. The analysis looks at reemployed respondents with regard to a dichotomous dependent variable (here: "dep1"). I am working on this problem for a while now, but I cannot find a definite solution and I am grateful for any ideas on how to approach this.
The problem is that I need to attribute the spell length to the dependent variable. I think an illlustration of my dataset will show why this is difficult:
This is certainly a worst-case scenario in my data, but many of these cases exists and I am trying to account for such cases in my approach. Down below is the -dataex- version of the whole observation period of the person.
ID | Year | Reemployed (1=yes) | Month of interview | Temp1 | Temp2 | Temp3 | Temp4 | Temp5 | Temp6 | Temp7 | Temp8 | Temp9 | Temp10 | Temp11 | Temp12 | Dependent variable (dummy) | |||
1 | 2002 | 0 | 4 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | . | |||
1 | 2003 | 0 | 4 | -2 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | |||
1 | 2004 | 1 | 3 | 1 | -2 | -2 | 1 | 1 | -2 | -2 | 1 | 1 | -2 | 1 | -2 | 0 | |||
1 | 2005 | 1 | 7 | 1 | 1 | 1 | 1 | 1 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 | |||
1 | 2006 | 0 | 8 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 | |||
1 | 2007 | 0 | 7 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 |
The problem: I can get the length of each spell reshaping to long-format to person-year-month and using -tsspell- counting the length of each spell. But I then would need to tell Stata to which year (where the dependent variable is) the spell length belongs. And since people like to one above often have multiple unemployment spells in a year; I can only think of one approach to create the length variable and attribute it to the corresponding year in which my dependent variable was asked.
But first let me show you how, in the end, it should look:
ID | Year | Reemployed (1=yes) | Month of interview | Temp1 | Temp2 | Temp3 | Temp4 | Temp5 | Temp6 | Temp7 | Temp8 | Temp9 | Temp10 | Temp11 | Temp12 | Dependent variable (dummy) | Length of last spell | ||
1 | 2002 | 0 | 4 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | . | |||
1 | 2003 | 0 | 4 | -2 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | |||
1 | 2004 | 1 | 3 | 1 | -2 | -2 | 1 | 1 | -2 | -2 | 1 | 1 | -2 | 1 | -2 | 0 | 12 | ||
1 | 2005 | 1 | 7 | 1 | 1 | 1 | 1 | 1 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 | 5 | ||
1 | 2006 | 0 | 8 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 | |||
1 | 2007 | 0 | 7 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | -2 | 0 |
This is where I stumble:
1) I cannot really think of a way to start the loop. Because a -while- loop seems to not work, because there is no ongoing criteria I could set -while- to TRUE. Like, e.g. I cannot say "while temp`n' == 1", because the loop starts often with "temp`n' == -2". But I need a way to tell the loop that I should start at the month of interview and then count until "-2" appears again.
2) Can Stata count over rows? So once it finishes e.g. at "temp1", it changes to "L.temp12"?
Really, I am grateful for every input, because I am slowly a little bit lost whether I might just not be able to control for spell length. If you think, btw, that this problem is much easier to handle in another program like R, please let me know, because I would then switch there.
Thank you very much!
-----
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long pid int syear float reemp byte(pmonin temp1 temp2 temp3 temp4 temp5 temp6 temp7 temp8 temp9 temp10 temp11 temp12 dep1) 1 1993 0 6 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 1994 1 4 -2 -2 1 1 1 1 1 1 1 1 1 1 . 1 1995 0 3 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 1996 0 5 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 1 1997 0 2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 1 1998 1 4 -2 -2 -2 -2 -2 -2 1 -2 -2 -2 -2 -2 . 1 1999 0 5 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2000 0 2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 1 2001 0 2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2002 0 4 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2003 0 4 -2 1 1 1 1 1 1 1 1 1 1 1 0 1 2004 1 3 1 -2 -2 1 1 -2 -2 1 1 -2 1 -2 0 1 2005 1 7 1 1 1 1 1 -2 -2 -2 -2 -2 -2 -2 0 1 2006 0 8 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 1 2007 0 7 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 0 1 2008 0 6 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2009 0 7 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2010 1 7 -2 -2 -2 -2 -2 1 1 -2 1 1 -2 1 . 1 2011 1 7 1 1 1 1 1 1 1 1 1 1 1 1 . 1 2012 0 4 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2013 0 6 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . 1 2014 0 7 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 . end label values pmonin pmonin label def pmonin 2 "[2] Februar", modify label def pmonin 3 "[3] March", modify label def pmonin 4 "[4] April", modify label def pmonin 5 "[5] May", modify label def pmonin 6 "[6] June", modify label def pmonin 7 "[7] July", modify label def pmonin 8 "[8] August", modify
Comment