Creating duration of marriage variable
I am trying to create a variable denoting duration of current marriage (years). The only wave with information on year of marriage is wave b. I am therefore attempting to create a variable with years of marriage which, in each year, gives the current year - year of marriage, provided that the individual is still married. I also want to include years married for individuals who are not married at the start of the bhps sampling, but marry between 1991-2008.
I have attempted to do this with the following Stata codes:
use variables lmary (from wave b - bmarriag), mastat and mlchy4 (from indresp files waves a-r and c-r)
by pid (wave), sort: replace lmary = lmary[_n-1] if lmary >= .
by pid (wave), sort: replace lmary = lmary[_n+1] if lmary >= .
by pid (wave), sort: replace mlchy4 = mlchy4[_n-1] if mlchy4 >= .
create variable that will give years married when 2-digit year of marriage (lmary) is subtracted from it
gen yy1900 = year - 1900
drop if individual was married before 1991 but is not married at the start of the bhps (1991)
drop if lmary<1991&lmary!=.&mastat!=1&wave==1
create years married variable for those who became married over the sample
gen yearsmarried = (year - mlchy4) if mastat==1
recode missing years married observations for those who were married before the sample, provided they are still married
if mastat==1 recode yearsmarried .=(yy1900-lmary)
However, Stata reports error: invalid name
Could someone please let me know what I am doing wrong and if there is a better/simpler way for me to create this variable? Thank you.