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


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 vage3=vage2*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
 



gen mage2=mage*mage
gen mage3=mage2*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
 



preserve
clear
gen beta=.
gen lower=.
gen upper=.
gen depvar=""
gen time=.
save "Timing_childcare_ce_fathers.dta", replace
restore

xtset vnr syear

foreach var of varlist  mtime_childcare mtime_childcare_sat mtime_childcare_sun{
	foreach num of numlist 1/7{
		qui xi: xtivreg2 l`num'.`var' vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)

		preserve
			use "Timing_childcare_ce_fathers.dta", clear
			local nplus = _N +1   
			set obs `nplus' 
	
			replace time=-`num' in l
			replace beta=_b[vretired] in l
			replace  lower= _b[vretired]-invnormal(0.975)*_se[vretired] in l
			replace  upper= _b[vretired]+invnormal(0.975)*_se[vretired] in l
			replace depvar="`var'" in l
			save "Timing_childcare_ce_fathers.dta", replace
	restore

	}

	qui xi: xtivreg2 `var' vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)

	preserve
		use "Timing_childcare_ce_fathers.dta", clear
		local nplus = _N +1   
		set obs `nplus' 
		replace time=0 in l
		replace beta=_b[vretired] in l
		replace  lower= _b[vretired]-invnormal(0.975)*_se[vretired] in l
		replace  upper= _b[vretired]+invnormal(0.975)*_se[vretired] in l
		replace depvar="`var'" in l
		save "Timing_childcare_ce_fathers.dta", replace
	restore

	foreach num of numlist 1/7{
		qui xi: xtivreg2 f`num'.`var' vage vage2 i.vmonth i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70, fe partial(i.vmonth i.syear) cluster(vnr)

		preserve
			use "Timing_childcare_ce_fathers.dta", clear
			local nplus = _N +1   
			set obs `nplus' 
	
			replace time=`num' in l
			replace beta=_b[vretired] in l
			replace  lower= _b[vretired]-invnormal(0.975)*_se[vretired] in l
			replace  upper= _b[vretired]+invnormal(0.975)*_se[vretired] in l
			replace depvar="`var'" in l
			save "Timing_childcare_ce_fathers.dta", replace
	restore

	}	
}	




preserve
clear
gen beta=.
gen lower=.
gen upper=.
gen depvar=""
gen time=.
save "Timing_childcare_ce_mothers.dta", replace
restore

xtset mnr syear

foreach var of varlist  vtime_childcare vtime_childcare_sat vtime_childcare_sun{
	foreach num of numlist 1/7{
		qui xi: xtivreg2 l`num'.`var' mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)

		preserve
			use "Timing_childcare_ce_mothers.dta", clear
			local nplus = _N +1   
			set obs `nplus' 
	
			replace beta=_b[mretired] in l
			replace  lower= _b[mretired]-invnormal(0.975)*_se[mretired] in l
			replace  upper= _b[mretired]+invnormal(0.975)*_se[mretired] in l
			replace time=-`num' in l
			replace depvar="`var'" in l
			save "Timing_childcare_ce_mothers.dta", replace
		restore

	}

	qui xi: xtivreg2 `var' mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)

	preserve
		use "Timing_childcare_ce_mothers.dta", clear
		local nplus = _N +1   
		set obs `nplus' 
		
		replace beta=_b[mretired] in l
		replace  lower= _b[mretired]-invnormal(0.975)*_se[mretired] in l
		replace  upper= _b[mretired]+invnormal(0.975)*_se[mretired] in l
		replace time=0 in l
		replace depvar="`var'" in l
		save "Timing_childcare_ce_mothers.dta", replace
	restore
	foreach num of numlist 1/7{
		qui xi: xtivreg2 f`num'.`var' mage mage2 i.mmonth i.syear (mretired = mdisc60) 					if mage>=50 & mage<=70, fe partial(i.mmonth i.syear) cluster(mnr)

		preserve
			use "Timing_childcare_ce_mothers.dta", clear
			local nplus = _N +1   
			set obs `nplus' 
	
			replace beta=_b[mretired] in l
			replace  lower= _b[mretired]-invnormal(0.975)*_se[mretired] in l
			replace  upper= _b[mretired]+invnormal(0.975)*_se[mretired] in l
			replace time=`num' in l
			replace depvar="`var'" in l
			save "Timing_childcare_ce_mothers.dta", replace
		restore

	}	
}	
