Derivation
variable: stepdate (table[s]: crs_residency_episodes_clean_poly [derived table])
variable: idmoth (table[s]: parents_lafd [derived table])
variable: idfath (table[s]: parents_lafd [derived table])
variable: m_lastAC (table[s]: parents_lafd [derived table])
variable: m_firstDC (table[s]: parents_lafd [derived table])
variable: f_lastAC (table[s]: parents_lafd [derived table])
variable: f_firstDC (table[s]: parents_lafd [derived table])
variable: event_date (table[s]: indices])
variable: adl (table[s]: indices])
<vital status for people is summarised from indices, crs data and reports of parental status, these are then merged into the main dataset for wives and husbands>
use ${gendata}\crs_residency_episodes_clean_nopoly, clear
bys ident: egen lastincrs=max(stopdate)
bys ident (stopdate): gen laststop=stoptype[_N]
format %td lastincrs
keep ident lastincrs laststop
duplicates drop
gen crs=1
save ${tempfiles}\allcrsmembers, replace
gen adl=1 if laststop==1 | laststop==4
replace adl=2 if laststop==2
rename lastincrs intdate
save ${tempfiles}\adlcrs, replace
*get listing of last alive and death dates from parents
use idmoth idfath m_lastAC m_firstDC f_lastAC f_firstDC using ${gendata}\parents_lafd, clear
expand 2, gen(exp)
drop if idmoth=="" & idfath==""
gen ident=idmoth if exp==0
replace ident=idfath if exp==1
gen lastA=m_lastAC if exp==0
replace lastA=f_lastAC if exp==1
gen firstD=m_firstDC if exp==0
replace firstD=f_firstDC if exp==1
keep ident lastA firstD
drop if ident==""
duplicates drop
save ${tempfiles}\parslafd, replace
*add in data from indices
use ${newstata}\indices, clear
replace intdate=event_date if event_date~=.
append using ${tempfiles}\adlcrs
expand 2 if adl==2, gen(exp)
replace adl=1 if exp==1
keep ident adl intdate
drop if adl>=3
bys ident adl: egen lastA=max(intdate) if adl==1
bys ident adl: egen firstD=min(intdate) if adl==2
append using ${tempfiles}\parslafd
bys ident: egen lastalive=max(lastA)
bys ident: egen firstdead=min(firstD)
keep ident lastalive firstdead
format %td lastalive firstdead
replace firstdead=. if lastalive>firstdead & lastalive~=.
duplicates drop
save ${tempfiles}\indices_lafd, replace