Hello!
I would like to generate a new variable that shows the sum of months of unemployment (1-12) for each three year time period (1984-1986, 1985-1987 etc) throughout the time period 1984 to 2020 for each person/observation. The variable should show the number of months a person is unemployed for each three year time period. For example if a person x was unemployed for 2 months in 1984, 4 months in 1985 and 5 months in 1986 their total of unemployment for the time period 1984-1986 would be 11 months. Consequently, if person x was unemployed for 0 months in 1987 their total of unemployed months in the three year period of 1985-1987 would fall to 9 months.
syear_01: survey year, only taking values between 1984 and 2020
pid_01: personal identification number
kal1d02_01: shows the months of unemployment a person experienced in the previous year.
eg. the new variable should show that pid 102 would have a value of 5 months of unemployment in the time period 1986-1988 and have a value of 0 months of unemployment in the time period 1987-1989.
I have tried with this command:
bys pid_01 (syear_01): egen total = total( kal1d02_01 ) if inrange(syear_01, 1984, 2020)
bys pid_01: egen wanted= max(total)
drop total
bys pid_01 (syear_01): replace wanted= 0 if _n!=_N
But this just summates over the whole period from 1984 and 2020, rather than the three year periods I am aiming to make.
Does anyone know how to create a variable such as this?
Thank you in advance.
Best Wishes,
Max
I would like to generate a new variable that shows the sum of months of unemployment (1-12) for each three year time period (1984-1986, 1985-1987 etc) throughout the time period 1984 to 2020 for each person/observation. The variable should show the number of months a person is unemployed for each three year time period. For example if a person x was unemployed for 2 months in 1984, 4 months in 1985 and 5 months in 1986 their total of unemployment for the time period 1984-1986 would be 11 months. Consequently, if person x was unemployed for 0 months in 1987 their total of unemployed months in the three year period of 1985-1987 would fall to 9 months.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input double syear_01 long pid_01 byte kal1d02_01 1984 101 0 1985 101 0 1986 101 0 1987 101 0 1988 101 0 1989 101 0 1984 102 0 1985 102 0 1986 102 5 1987 102 0 1988 102 0 1989 102 0 1984 103 0 1985 103 0 1986 103 0 1987 103 0 1984 201 0 1985 201 0 1986 201 0 1987 201 0 1988 201 0 1989 201 0 1990 201 0 1991 201 0 1992 201 0 1993 201 6 1994 201 6 1995 201 0 1996 201 0 1997 201 0 1998 201 0 1999 201 0 2000 201 0 2001 201 0 2002 201 0 2003 201 0 2004 201 0 2005 201 0 2006 201 0 2007 201 0 2008 201 0 1985 202 0 1986 202 0 1987 202 0 1985 203 0 1986 203 0 1987 203 0 1988 203 0 1989 203 0 1990 203 0 1991 203 0 1992 203 0 1993 203 2 1994 203 12 1995 203 0 1996 203 0 1997 203 3 1998 203 3 1999 203 3 2000 203 0 2001 203 0 2002 203 0 2003 203 0 2004 203 0 2005 203 0 2006 203 0 2007 203 0 2008 203 0 1984 301 0 1985 301 0 1986 301 0 1987 301 0 1988 301 0 1989 301 0 1990 301 0 1991 301 0 1992 301 0 1993 301 0 1984 302 0 1985 302 0 1986 302 0 1987 302 0 1988 302 3 1989 302 0 1990 302 0 1991 302 0 1992 302 0 1993 302 0 1984 401 0 1985 401 0 1986 401 0 1984 501 0 1985 501 0 1986 501 8 1987 501 0 1988 501 0 1989 501 0 1990 501 0 1991 501 0 1992 501 0 end
syear_01: survey year, only taking values between 1984 and 2020
pid_01: personal identification number
kal1d02_01: shows the months of unemployment a person experienced in the previous year.
eg. the new variable should show that pid 102 would have a value of 5 months of unemployment in the time period 1986-1988 and have a value of 0 months of unemployment in the time period 1987-1989.
I have tried with this command:
bys pid_01 (syear_01): egen total = total( kal1d02_01 ) if inrange(syear_01, 1984, 2020)
bys pid_01: egen wanted= max(total)
drop total
bys pid_01 (syear_01): replace wanted= 0 if _n!=_N
But this just summates over the whole period from 1984 and 2020, rather than the three year periods I am aiming to make.
Does anyone know how to create a variable such as this?
Thank you in advance.
Best Wishes,
Max
Comment