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

*Generate variables
gen age2=age*age
gen age3=age2*age

gen vage2=vage*vage
gen vage3=vage2*vage
gen vage4=vage3*vage
gen vage5=vage4*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 vage_disc60=vage*vdisc60 
gen vage2_disc60=vage2*vdisc60 

*Generate alternative definitions of retirement based on time use and pension receipt 
gen vretired_hours=.
replace vretired_hours=0 if vtime_job>=4 & vtime_job!=.
replace vretired_hours=1 if vtime_job<4 & vtime_job!=.

gen vretired_pension=.
replace vretired_pension=0 if vpension==0
replace vretired_pension=1 if vpension==1

sort pid syear
xtset pid syear
gen fbirth=f.birth

*Generate new running variable centered around the discontinuity
gen vdisc=60+1/12
replace vdisc=60+2/12 if vgebjahr==1952 & vgebmonat==1
replace vdisc=60+3/12 if vgebjahr==1952 & vgebmonat==2
replace vdisc=60+4/12 if vgebjahr==1952 & vgebmonat==3
replace vdisc=60+5/12 if vgebjahr==1952 & vgebmonat==4
replace vdisc=60+6/12 if vgebjahr==1952 & vgebmonat==5
replace vdisc=60+7/12 if vgebjahr==1952 & vgebmonat>=6 & vgebmonat<=12
replace vdisc=60+8/12 if vgebjahr==1953 
replace vdisc=60+9/12 if vgebjahr==1954
replace vdisc=60+10/12 if vgebjahr==1955
replace vdisc=60+11/12 if vgebjahr==1956
replace vdisc=61 if vgebjahr==1957


gen vage_run=vage-vdisc
gen vage_run_disc=cond(vage_run>=0 & vage_run!=.,vage_run,cond(vage_run<0 & vage_run!=.,0,.))
gen vage2_run=vage_run*vage_run
gen vage2_run_disc=cond(vage_run>=0 & vage_run!=.,vage2_run,cond(vage_run<0 & vage_run!=.,0,.))
gen vthresh=cond(vage_run>=0 & vage_run!=.,1,cond(vage_run!=.,0,.))




eststo: xi: xtivreg2 fbirth age age2 vage_run vage_run_disc i.month i.syear (vretired = vthresh) 	 						if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_10_)

eststo: xi: xtivreg2 fbirth age age2 vage_run vage2_run vage_run_disc vage2_run_disc i.month i.syear (vretired = vthresh)  						if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_14_)


eststo: xi: xtivreg2 fbirth age age2 vage vage2 vage3 vage4 i.month i.syear (vretired = vdisc60) 				if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_1_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 vage3 i.month i.syear (vretired = vdisc60) 						if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_2_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=53 & vage<=67 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_3_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=55 & vage<=65 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_4_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=57 & vage<=63 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_5_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=50 & vage<=70 & age>=20 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_6_)
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=50 & vage<=70 & age>=17 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_7_)
replace vretired=vretired_hours
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_8_)
replace vretired=vretired_pension
eststo: xi: xtivreg2 fbirth age age2 vage vage2 i.month i.syear (vretired = vdisc60) 							if vage>=50 & vage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_9_)




estout est1 est2 est3 est4 est5 est6 est7 est8 est9 est10 est11  using "${output_path}\Results\Table6.csv" , keep(vretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed) starlevels(* 0.10 ** 0.050 *** 0.010)  replace 
estout _10_vretired _14_vretired _1_vretired _2_vretired _3_vretired _4_vretired _5_vretired _6_vretired _7_vretired _8_vretired _9_vretired using "${output_path}\Results\Table6.csv" , keep(vthresh vdisc60) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(none) collabels(none)  style(fixed) starlevels(* 0.10 ** 0.050 *** 0.010)  append
estout est1 est2 est3 est4 est5 est6 est7 est8 est9 est10 est11  using "${output_path}\Results\Table6.csv" ,keep() cells(none) mlabels(none) collabels(none)  style(fixed)  stats(widstat N) starlevels(* 0.10 ** 0.050 *** 0.010)  append 


eststo clear





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



gen mage_disc60=mdisc60*mage
gen mage2_disc60=mdisc60*mage2
gen mage3_disc60=mdisc60*mage3


gen mretired_hours=.
replace mretired_hours=0 if mtime_job>=4 & mtime_job!=.
replace mretired_hours=1 if mtime_job<4 & mtime_job!=.

gen mretired_pension=.
replace mretired_pension=0 if mpension==0
replace mretired_pension=1 if mpension==1


gen mdisc=60+1/12
replace mdisc=60+2/12 if mgebjahr==1952 & mgebmonat==1
replace mdisc=60+3/12 if mgebjahr==1952 & mgebmonat==2
replace mdisc=60+4/12 if mgebjahr==1952 & mgebmonat==3
replace mdisc=60+5/12 if mgebjahr==1952 & mgebmonat==4
replace mdisc=60+6/12 if mgebjahr==1952 & mgebmonat==5
replace mdisc=60+7/12 if mgebjahr==1952 & mgebmonat>=6 & mgebmonat<=12
replace mdisc=60+8/12 if mgebjahr==1953 
replace mdisc=60+9/12 if mgebjahr==1954
replace mdisc=60+10/12 if mgebjahr==1955
replace mdisc=60+11/12 if mgebjahr==1956
replace mdisc=61 if mgebjahr==1957

gen mage_run=mage-mdisc
gen mage_run_disc=cond(mage_run>=0 & mage_run!=.,mage_run,cond(mage_run<0 & mage_run!=.,0,.))
gen mage2_run=mage_run*mage_run
gen mage2_run_disc=cond(mage_run>=0 & mage_run!=.,mage2_run,cond(mage_run<0 & mage_run!=.,0,.))
gen mthresh=cond(mage_run>=0 & mage_run!=.,1,cond(mage_run!=.,0,.))





eststo: xi: xtivreg2 fbirth age age2 mage_run mage_run_disc i.month i.syear (mretired = mthresh)						if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_10_)

eststo: xi: xtivreg2 fbirth age age2 mage_run mage2_run mage_run_disc mage2_run_disc i.month i.syear (mretired = mthresh)						if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_14_)

eststo: xi: xtivreg2 fbirth age age2 mage mage2 mage3 mage4 i.month i.syear (mretired = mdisc60) 				if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_1_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 mage3 i.month i.syear (mretired = mdisc60) 						if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_2_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=53 & mage<=67 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_3_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=55 & mage<=65 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_4_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=57 & mage<=63 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_5_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=50 & mage<=70 & age>=20 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_6_)
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=50 & mage<=70 & age>=17 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_7_)
replace mretired=mretired_hours
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_8_)
replace mretired=mretired_pension
eststo: xi: xtivreg2 fbirth age age2 mage mage2 i.month i.syear (mretired = mdisc60) 							if mage>=50 & mage<=70 & age>=25 & age<=40, fe cluster(pid) partial(i.month i.syear) first savefirst savefprefix(_9_)

estout est1 est2 est3 est4 est5 est6 est7 est8 est9 est10 est11 using "${output_path}\Results\Table6.csv" , keep(mretired) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(,depvars) style(fixed) starlevels(* 0.10 ** 0.050 *** 0.010)  append 
estout _10_mretired _14_mretired _1_mretired _2_mretired _3_mretired _4_mretired _5_mretired _6_mretired _7_mretired _8_mretired _9_mretired using "${output_path}\Results\Table6.csv" , keep(mthresh mdisc60) cells(b(star fmt(3)) se(par(`"="("' `")""'))) mlabels(none) collabels(none)  style(fixed) starlevels(* 0.10 ** 0.050 *** 0.010)  append
estout est1 est2 est3 est4 est5 est6 est7 est8 est9 est10 est11  using "${output_path}\Results\Table6.csv" ,keep() cells(none) mlabels(none) collabels(none)  style(fixed)  stats(widstat N) starlevels(* 0.10 ** 0.050 *** 0.010)  append 



eststo clear

