clear
clear matrix

set more off
eststo clear
cd "${output_path}\tmp\"
use "Linked_data.dta", clear


xtset, clear
keep if age>=25 & age<=40

*The data is transformed from child-parent level to parent level by only retaining variables with parental information and then dropping duplicate observations
keep v* syear m*
drop vil* mil* mother_near 
drop merge*

drop month mother_known migback




duplicates drop

set more off


gen vage2=vage*vage


gen vdisc60=cond((vage+0.001)>=(60+1/12),1,0)
replace vdisc60=cond((vage+0.001)>=(60+2/12),1,0) if vgebjahr==1952 & vgebmonat==1
replace vdisc60=cond((vage+0.001)>=(60+3/12),1,0) if vgebjahr==1952 & vgebmonat==2
replace vdisc60=cond((vage+0.001)>=(60+4/12),1,0) if vgebjahr==1952 & vgebmonat==3
replace vdisc60=cond((vage+0.001)>=(60+5/12),1,0) if vgebjahr==1952 & vgebmonat==4
replace vdisc60=cond((vage+0.001)>=(60+6/12),1,0) if vgebjahr==1952 & vgebmonat==5
replace vdisc60=cond((vage+0.001)>=(60+7/12),1,0) if vgebjahr==1952 & vgebmonat>=6 & vgebmonat<=12
replace vdisc60=cond((vage+0.001)>=(60+8/12),1,0) if vgebjahr==1953 
replace vdisc60=cond((vage+0.001)>=(60+9/12),1,0) if vgebjahr==1954
replace vdisc60=cond((vage+0.001)>=(60+10/12),1,0) if vgebjahr==1955
replace vdisc60=cond((vage+0.001)>=(60+11/12),1,0) if vgebjahr==1956
replace vdisc60=cond((vage+0.001)>=61,1,0) if vgebjahr==1957
 

xtset vnr syear
foreach var of varlist vtime_*{
gen f`var'=f.`var'
}

gen mage2=mage*mage


gen mdisc60=cond((mage+0.001)>=(60+1/12),1,0)
replace mdisc60=cond((mage+0.001)>=(60+2/12),1,0) if mgebjahr==1952 & mgebmonat==1
replace mdisc60=cond((mage+0.001)>=(60+3/12),1,0) if mgebjahr==1952 & mgebmonat==2
replace mdisc60=cond((mage+0.001)>=(60+4/12),1,0) if mgebjahr==1952 & mgebmonat==3
replace mdisc60=cond((mage+0.001)>=(60+5/12),1,0) if mgebjahr==1952 & mgebmonat==4
replace mdisc60=cond((mage+0.001)>=(60+6/12),1,0) if mgebjahr==1952 & mgebmonat==5
replace mdisc60=cond((mage+0.001)>=(60+7/12),1,0) if mgebjahr==1952 & mgebmonat>=6 & mgebmonat<=12
replace mdisc60=cond((mage+0.001)>=(60+8/12),1,0) if mgebjahr==1953 
replace mdisc60=cond((mage+0.001)>=(60+9/12),1,0) if mgebjahr==1954
replace mdisc60=cond((mage+0.001)>=(60+10/12),1,0) if mgebjahr==1955
replace mdisc60=cond((mage+0.001)>=(60+11/12),1,0) if mgebjahr==1956
replace mdisc60=cond((mage+0.001)>=61,1,0) if mgebjahr==1957
 

xtset mnr syear
foreach var of varlist mtime_*{
gen f`var'=f.`var'
}


*Father's retirement and father's time use
xtset vnr syear
qui eststo: xi: xtivreg2 fvtime_childcare 		vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)
qui eststo: xi: xtivreg2 fvtime_childcare_sat	vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)
qui eststo: xi: xtivreg2 fvtime_childcare_sun	vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)

*Father's retirement and mother's time use
qui eststo: xi: xtivreg2 fmtime_childcare 		vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)
qui eststo: xi: xtivreg2 fmtime_childcare_sat 	vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)
qui eststo: xi: xtivreg2 fmtime_childcare_sun 	vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)

estout _all using "${output_path}\Results\Table5.csv" , keep(vretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed)  stats(widstat N) ///
starlevels(* 0.10 ** 0.050 *** 0.010)  replace
eststo clear

xtset mnr syear
*Mother's retirement and father's time use
qui eststo: xi: xtivreg2 fvtime_childcare 		mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)
qui eststo: xi: xtivreg2 fvtime_childcare_sat 	mage mage2 i.mmonth i.syear (mretired = mdisc60)						if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)
qui eststo: xi: xtivreg2 fvtime_childcare_sun 	mage mage2 i.mmonth i.syear (mretired = mdisc60)						if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)

*Mother's retirement and mother's time use
qui eststo: xi: xtivreg2 fmtime_childcare 		mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)
qui eststo: xi: xtivreg2 fmtime_childcare_sat 	mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)
qui eststo: xi: xtivreg2 fmtime_childcare_sun 	mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)

estout _all using "${output_path}\Results\Table5.csv" , keep(mretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed)  stats(widstat N) ///
starlevels(* 0.10 ** 0.050 *** 0.010) append
eststo clear
