Hi all,

What I am trying to do is identify all people who have missing observations of two or more years between reporting of being single and being married. This is based on panel data 1984-2015. T tried using the below command (pid=perosnal ID, marstatus =1 if the person is married):

by pid (syear), sort: gen spell_num = sum((missing(marstatus) != missing(marstatus[_n-1])))
by pid spell_num (syear), sort: gen spell_duration = _N
by pid, sort: egen longest_missing_spell = max(cond(missing(marstatus), spell_duration, .))

by pid, sort: egen ever_married = max(marstatus)
by pid, sort: gen single_married = Single == 1 | Married==1

Am I on the right track ?

Thank you !

Regards,
Gabriella

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long pid int syear byte marstatus
101 1984 1
101 1985 1
101 1986 1
101 1987 1
101 1988 1
101 1989 1
102 1984 1
102 1985 1
102 1986 1
102 1987 1
102 1988 1
102 1989 1
103 1984 2
103 1985 2
103 1986 2
103 1987 2
201 1984 .
201 1985 .
201 1986 .
201 1987 .
201 1988 .
201 1989 .
201 1990 .
201 1991 .
201 1992 .
201 1993 .
201 1994 .
201 1995 .
201 1996 .
201 1997 .
201 1998 .
201 1999 .
201 2000 4
201 2001 4
201 2002 3
201 2003 4
201 2004 4
201 2005 4
201 2006 4
201 2007 4
201 2008 4
202 1984 2
202 1985 2
202 1986 2
202 1987 2
203 1984 2
203 1985 2
203 1986 2
203 1987 2
203 1988 2
203 1989 2
203 1990 2
203 1991 2
203 1992 2
203 1993 2
203 1994 2
203 1995 2
203 1996 2
203 1997 2
203 1998 2
203 1999 2
203 2000 2
203 2001 2
203 2002 2
203 2003 2
203 2004 2
203 2005 2
203 2006 2
203 2007 2
203 2008 2
204 1990 .
204 1991 .
204 1992 .
204 1993 .
204 1994 .
301 1984 2
301 1985 1
301 1986 1
301 1987 1
301 1988 1
301 1989 1
301 1990 1
301 1991 1
301 1992 1
301 1993 1
302 1984 2
302 1985 1
302 1986 1
302 1987 1
302 1988 1
302 1989 1
302 1990 1
302 1991 1
302 1992 1
302 1993 1
303 1984 .
303 1985 .
303 1986 .
303 1987 .
303 1988 .
end
label values marstatus d11104
label def d11104 1 "[1] Married        1", modify
label def d11104 2 "[2] Single         2", modify
label def d11104 3 "[3] Widowed        3", modify
label def d11104 4 "[4] Divorced       4", modify