*Tables for the paper "The Opportunity Costs of Mandatory Military Service: Evidence from a Draft Lottery"

*Table 1 Summary Statistics


use "opp-cost-master", clear
keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12
gen lkearn=log(kearn)
drop unikpnr
by pnr, sort : gen unikpnr=_n
keep if unikpnr==1


label var afqt "AFQT"
label var height "Height (cm)"
label var yob "Birth year"
label var mob " Birth month"
label var immigrant "Son of immigrant"
label var fromsingleparentfamily "Raised in single-parent family"
label var outofhomecare "Placed in out-of-home care"
label var etdisp " Household income at age 15 (DKK)"
label var meduc "Mother's years of schooling"
label var feduc "Father's years of schooling"
label var birthweight "Birth weight (gr)"

macro def backgroundvar2 height afqt yob mob fromsingleparentfamily outofhomecare immigrant birthweight etdisp meduc feduc
estimates drop _all
estpost sum $backgroundvar2
est sto A
estpost sum $backgroundvar2 if served==1
est sto B
estpost sum $backgroundvar2 if served==0
est sto C
esttab A B C, cells("mean(fmt(3)) sd(fmt(3))") 
esttab  A B C  using "tab1.txt", tex cells(mean(fmt(2)) sd(fmt(2) par(( )))) nonum label collabels ("mean" "sd") mtitle ("Full sample" "Served" "Not served" ) replace





*Table 2 - draft assignment randomization balancing check
*Table A2 - lottery assignment randomization balancing check

use pnr qafqt age etdisp meduc feduc yob mob shy sessan_year year height height2 afqt afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight drawlo served learn lotterydraw using "opp-cost-master", clear

keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"

global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane"
global covb1 "fromsingleparentfamily outofhomecare dane letdisp meduc feduc"
global covc "letdisp meduc feduc birthweight birthweight2"

replace birthweight=birthweight/1000
gen birthweight2=birthweight^2
label variable birthweight "birthweight"
label variable birthweight2 "birthweight2"

label var height "height"
label var afqt "AFQT"
label var height2 "height2"
label var afqt2 "AFQT2"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000

prog define balsave
mat first=e(first) 
mat r=first["SWr2",1] 
mat f=first["SWF",1] 
mat p=first["SWFp",1]
scalar pp=p[1,1]
scalar r=r[1,1]
scalar f=f[1,1]
estadd scalar f=f:b`1'
estadd scalar pp=pp:b`1' 
estadd scalar r=r:b`1'
end


*balancing by different covariates
prog define balance
eststo clear
eststo b1: reg drawlo $cova $covb, robust
xi: ivreg2 served (which=$covb) $cova, first ffirst
balsave 1
eststo b2: reg which $cova $covb $covc, robust
xi: ivreg2 served (which=$covb $covc) $cova, first ffirst 
balsave 2
eststo b3: reg which $cova $covb if countlearn>0, robust
xi: ivreg2 served (which= $covb) $cova if countlearn>0, first ffirst 
balsave 3
eststo b4: reg which $cova $covb $covc if countlearn>0, robust
xi: ivreg2 served (which=$covb $covc) $cova if countlearn>0, first ffirst
balsave 4
esttab b1 b2 b3 b4 using "tab2`1'.txt", tex se replace scalar ("f F-Statistic" "pp F-Stat p-value" "r Partial-R2") ///
b(%06.5f) starlevels(* 0.10 ** 0.05 *** 0.010) mtitles ("Total I" "Total II" "Estim I" "Estim II") label keep($covb $covc) obslast
end


*balancing by afqt quartiles
prog define balanceq
eststo b`1': reg drawlo $cova $covb1 if qafqt==`1' & countlearn>0, robust
xi: ivreg2 served (which=$covb1) $cova if qafqt==`1' & countlearn>0, first ffirst
balsave `1'
end


*balancing on draft or lottery number
egen countlearn=count(learn), by(pnr)
keep if unikpnr==1
gen which=drawlo
balance 1
replace which=lotterydraw
balance 2 




*Tab 3 - draft status, military status and earnings with draft status as instrument
*Tab A3 - draft status, military status and earnings with draft status and lottery number as instruments

use learn age etdisp pnr yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight drawlo served lotterydraw using "opp-cost-master", clear
prog drop _all

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"

global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end


*reduced form
eststo clear
forval y=0/4 {
  reg learn drawlo ${cov`y'}, cluster(pnr) 
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_main_rf.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

*ols
set more 1
eststo clear
forval y=0/4 {
  reg learn served ${cov`y'}, cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_main_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(served) obslast

*first stage
set more 1
eststo clear
forval y=0/4 {
  reg served drawlo ${cov`y'}, cluster (pnr)
  gen mn=served
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_main_fs.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_main_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast



*include lottery draw as instrument
*first stage
eststo clear
forval y=0/4 {
  reg served drawlo lotterydraw ${cov`y'}, cluster (pnr)
  gen mn=served
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_lottery_fs.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo lotterydraw) obslast

* instrumental variables
eststo clear
forval y=0/4 {
  gen mn=learn
  xi: ivreg2 learn (served=drawlo lotterydraw) ${cov`y'}, first ffirst cluster(pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab3_lottery_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast



* Table 4 Draft status, military service status and earning by AFQT score quartile
********************************************************************************
use "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

drop unikpnr
by pnr, sort : gen unikpnr=_n

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"
********************************************************************************
*Tab 4 IV
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):iv`Y'
	}
}
esttab iv1 iv2 iv3 iv4 using "tab4iv.txt", tex replace scalar //
("N_g N. clusters" "F_first First stage F-test")nomtitles se //
star(* 0.10 ** 0.05 *** 0.010) label keep(served) 

*Tab 4 firststage 
foreach Y of num 1/4 {
reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
}
esttab iv1 iv2 iv3 iv4 using "tab4fs.txt", tex replace mtitle //
("1st quartile" "2nd quartile" "3rd quartile" "Top quartile")se //
star(* 0.10 ** 0.05 *** 0.010)label keep(drawlo)

*Tab 4 OLS
set more 1
eststo clear
foreach Y of num 1/4 {
reg learn served $somecovariates if qafqt==`Y', cluster(pnr)robust
est store ols`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):ols`Y'
}

esttab ols1 ols2 ols3 ols4 using "tab4ols.txt", tex replace ar2 //
scalar("N_g N. clusters") nomtitles se star(* 0.10 ** 0.05 *** 0.010) //
label keep(served)

* tab 4 reduced form
eststo clear
foreach Y of num 1/4 {
reg learn drawlo $somecovariates if qafqt==`Y', cluster(pnr)robust
est store rf`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):rf`Y'
}

esttab rf1 rf2 rf3 rf4 using "tab4rf.txt", tex replace ar2 //
scalar("N_g N. clusters") nomtitles se star(* 0.10 ** 0.05 *** 0.010)//
label keep(drawlo)




* Table 5 Effects of military service on earnings across quartiles of the 
* distributions of background characteristics

set more 1

use pnr etdisp meduc feduc afqt height birthweight using "opp-cost-master", clear
duplicates drop pnr, force
for X in any etdisp meduc feduc afqt height birthweight: xtile qqX=X, n(4) \ tab qqX
compress
keep pnr qq*

joinby pnr using "opp-cost-master"
keep if learn!=.
keep if age >24

gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"

drop unikpnr
by pnr, sort : gen unikpnr=_n
********************************************************************************
*Panel A  Across household income quartiles
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qqetdisp==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo $somecovariates if qqetdisp==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):Q`Y'
		}
}
esttab Q1 Q2 Q3 Q4  using "tab5-panelA.txt", tex ///
replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
     
*Panel B Across father's education quartiles
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qqfeduc==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo $somecovariates if qqfeduc==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):Q`Y'
		}
}
esttab Q1 Q2 Q3 Q4  using "tab5-panelB.txt", tex ///
replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
    	 
*Panel C Across mother's education quartiles
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qqmeduc==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo $somecovariates if qqmeduc==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):Q`Y'
		}
}
esttab Q1 Q2 Q3 Q4  using "tab5-panelC.txt", tex ///
replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
     	 	 
*Panel D Across birthweight quartiles
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qqbirthweight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo $somecovariates if qqbirthweight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):Q`Y'
		}
}
esttab Q1 Q2 Q3 Q4 using "tab5-panelD.txt", tex ///
replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
     	
*Panel E Across height quartiles
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qqheight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo $somecovariates if qqheight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):Q`Y'
		}
}
esttab Q1 Q2 Q3 Q4 using "tab5-panelE.txt", tex ///
replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
     	
     	
     	
     	
*table 6 - effect of military service on schooling attainment and enrolment
*table a14 - effect of military service on schooling attainment and enrolment without AFD timing control

use learn age etdisp yob mob shy sessan_year height2 afqt2 height afqt pnr drawlo served educk25 educk26 educk27 educk28 educk29 educk30 qafqt aineduc25 aineduc28 aineduc30 meduc feduc using "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n

**********

global somecovariates "i.yob i.mob i.shy i.sessan_year height2 afqt2 height afqt"
global noshy "i.yob i.mob i.sessan_year height2 afqt2 height afqt"
global nosessan "i.yob i.mob i.shy height2 afqt2 height afqt"


label var drawlo "Draft status=1"
label var served "Service status=1"

duplicates drop pnr, force


*table a14
* Years of schooling

eststo clear
foreach Z of num 25 30 {
xi: ivreg2 educk`Z' (served=drawlo) $nosessan, cluster(pnr)
est store iv
quietly {
	reg served drawlo $nosessan, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv
		}

foreach Y of num 1/4 {
xi: ivreg2 educk`Z' (served=drawlo) $nosessan if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $nosessan if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
		}
}

esttab iv iv1 iv2 iv3 iv4 using "education_nosessan`Z'.txt", tex ///
replace scalar ("F_first First stage F-test")mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 
}


*table a14
*Studying at different age

eststo clear
foreach Z of num 25 28 30 {
xi: ivreg2 aineduc`Z' (served=drawlo) $nosessan, cluster(pnr)
est store iv
quietly {
	reg served drawlo $nosessan, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv
		}

foreach Y of num 1/4 {
xi: ivreg2 aineduc`Z' (served=drawlo) $nosessan if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $nosessan if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
		}
}

esttab iv iv1 iv2 iv3 iv4 using "ineduc_nosessan`Z'.txt", tex ///
replace scalar ("F_first First stage F-test")mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 
}



*table 6
* Years of schooling

eststo clear
foreach Z of num 25 30 {
xi: ivreg2 educk`Z' (served=drawlo) $somecovariates, cluster(pnr)
est store iv
quietly {
	reg served drawlo $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv
		}

foreach Y of num 1/4 {
xi: ivreg2 educk`Z' (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
		}
}

esttab iv iv1 iv2 iv3 iv4 using "education`Z'.txt", tex ///
replace scalar ("F_first First stage F-test")mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 
}


*table 6
*Studying at different age

eststo clear
foreach Z of num 25 28 30 {
xi: ivreg2 aineduc`Z' (served=drawlo) $somecovariates, cluster(pnr)
est store iv
quietly {
	reg served drawlo $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv
		}

foreach Y of num 1/4 {
xi: ivreg2 aineduc`Z' (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
		}
}

esttab iv iv1 iv2 iv3 iv4 using "ineduc`Z'.txt", tex ///
replace scalar ("F_first First stage F-test")mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 
}






* Table 7 Effect of military service on various outcomes by AFQT score quartile
use "opp-cost-master", clear

keep if learn!=.
keep if age >25
replace meduc=meduc/12
replace feduc=feduc/12

for Y in any before after:replace Yunemp=Yunemp/1000

global somecovariates "i.yob i.mob i.shy i.sessan_year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"
********************************************************************************
*Panel A. unemployment=1 at age 26
eststo clear
xi: ivreg2 afterunemp (served=drawlo) beforeunemp $somecovariates, first cluster(pnr)
est store full
quietly {
	reg served drawlo beforeunemp $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterunemp (served=drawlo) beforeunemp $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforeunemp $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4 using "tab7-panelA.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* Panel B. Bank balance in DKK at age 26
eststo clear
xi: ivreg2 afterbank (served=drawlo) beforebank $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforebank $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterbank (served=drawlo) beforebank $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforebank $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelB.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* Panel C. Criminal  convictions=1 for age 26-35
eststo clear
xi: ivreg2 aftercrime (served=drawlo) beforecrime $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforecrime $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 aftercrime (served=drawlo) beforecrime $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforecrime $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelC.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* Panel D. Hospitalization=1 for age 26-35
eststo clear
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforelpr $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforelpr $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelD.txt", tex ///
replace scalar ("F_first First stage F-test")mtitles ///
("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
  
* Panel E. Prescription medicine=1 for age 26-35
eststo clear
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforemedicine $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforemedicine $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelE.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
 
* Panel F. Psykiatric diagnosis=1 for age 26-35
eststo clear
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforepsykdiag $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforepsykdiag $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelF.txt", tex ///
replace scalar ("F_first First stage F-test") mtitles ///
("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
   
* Panel G. addiction
eststo clear
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforeaddiction $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates if qafqt==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforeaddiction $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "tab7-panelG.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)   
********************************************************************************
*Table A.1 Summary Statistics for other outcomes 

macro def backgroundvar3 *unemp *bank *crime *lpr *medicine *psykdiag *addiction
*LABEL
label var beforeunemp "Unemployment before AFD"
label var afterunemp "Unemployment at age 26"
label var beforebank "Bank balance in DKK before AFD"
label var afterbank "Bank balance at age 26"
label var beforecrime "Crime before AFD"
label var aftercrime "Crime convictions for age 26-35"
label var beforemedicine "Prescription medicine before AFD"
label var aftermedicine "Prescription medicine for age 26-35"
label var beforepsykdiag "Psychiatric diagnosis before AFD"
label var afterpsykdiag "Psychiatric diagnosis for age 26-35"
label var beforeaddiction "Addiction treatment before AFD"
label var afteraddiction "Addiction treatment for age 26-35"
label var beforelpr "Hospitalization before AFD"
label var afterlpr "Hospitalization for age 26-35"
estimates drop _all
estpost sum $backgroundvar3
est sto A
estpost sum $backgroundvar3 if served==1
est sto B
estpost sum $backgroundvar3 if served==0
est sto C
esttab A B C, cells("mean(fmt(3)) sd(fmt(3))") 

esttab  A B C  using "tabA1C1-C3.txt", tex cells(mean(fmt(3)) sd(fmt(3) ///
par(( )))) nonum label collabels ("mean" "sd") mtitle ("Full sample" "Served" "Not served" ) replace




*Table A10 - effect of military service on earnings by (3 year) birth cohort

use pnr qafqt drawlo served lotterydraw learn age etdisp meduc feduc yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane birthweight using "opp-cost-master", clear
prog drop _all

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"

global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn if e(sample)
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn if e(sample)
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end





* instrumental variables by year for ALL
set more 1
eststo clear
forval y=0/5 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov3} if yob>=(1976+`y') & yob<=(1978+`y'), first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 i5 using "tab_earn_by_3cohort_all.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1976-78" "1977-79" "1978-80" "1979-81" "1980-82" "1981-83") /// 
label keep(served) obslast


* instrumental variables by year for AFQTQ4
set more 1
eststo clear
forval y=0/5 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov3} if yob>=(1976+`y') & yob<=(1978+`y') & qafqt==4, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 i5 using "tab_earn_by_3cohort_afqtq4.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1976-78" "1977-79" "1978-80" "1979-81" "1980-82" "1981-83") ///  
label keep(served) obslast




*Table A11 - effect of military service on discounted earnings ages 25-30 by afqt score quartile

prog drop _all

use qafqt pnr age served drawlo lotterydraw learn etdisp yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear
keep if learn!=.
keep if age >24 & age<31
for R in num 0 3 5: gen dvR=0
for R in num 0 3 5: for A in num 25/30: replace dvR=exp(learn)/(1.0R^(A-24)) if age==A

gsort pnr -age
for R in num 0 3 5: qby pnr: gen pdvR=sum(dvR)
keep if age==25

gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end



prog define discounted
replace learn=log(pdv`1')
*ols
set more 1
eststo clear
forval y=1/4 {
  reg learn served ${cov2} if qafqt==`y', cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i1 i2 i3 i4 using "taba11_qpdv`1'_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
label keep(served) obslast
* instrumental variables
set more 1
eststo clear
forval y=1/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov2} if qafqt==`y', first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i1 i2 i3 i4 using "taba11_qpdv`1'_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") /// 
label keep(served) obslast
end


for R in num 0 3 5: discounted R


* Table A12 Draft status, military service status and earning by AFQT score 
* quartile-- excluding 4 months service
********************************************************************************
use "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

drop if spaer_year>2006

drop unikpnr
by pnr, sort : gen unikpnr=_n

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"
********************************************************************************
*Tab A12 IV
eststo clear
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):iv`Y'
	}
}
esttab iv1 iv2 iv3 iv4 using "taba12iv.txt", tex replace scalar //
("N_g N. clusters" "F_first First stage F-test")nomtitles se //
star(* 0.10 ** 0.05 *** 0.010) label keep(served) 

*Tab A12 firststage 
foreach Y of num 1/4 {
reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
}
esttab iv1 iv2 iv3 iv4 using "taba12fs.txt", tex replace mtitle //
("1st quartile" "2nd quartile" "3rd quartile" "Top quartile")se //
star(* 0.10 ** 0.05 *** 0.010)label keep(drawlo)

*Tab A12 OLS
set more 1
eststo clear
foreach Y of num 1/4 {
reg learn served $somecovariates if qafqt==`Y', cluster(pnr)robust
est store ols`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):ols`Y'
}

esttab ols1 ols2 ols3 ols4 using "taba12ols.txt", tex replace ar2 //
scalar("N_g N. clusters") nomtitles se star(* 0.10 ** 0.05 *** 0.010) //
label keep(served)

* tab A12 reduced form
eststo clear
foreach Y of num 1/4 {
reg learn drawlo $somecovariates if qafqt==`Y', cluster(pnr)robust
est store rf`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):rf`Y'
}

esttab rf1 rf2 rf3 rf4 using "taba12rf.txt", tex replace ar2 //
scalar("N_g N. clusters") nomtitles se star(* 0.10 ** 0.05 *** 0.010)//
label keep(drawlo)




*Table A.13 EFFECTS OF MILITARY SERVICE ON EARNINGS AT AGES 19-24



use "opp-cost-master", clear

keep if learn!=.
keep if age >18 & age <25
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"

drop unikpnr
by pnr, sort : gen unikpnr=_n
********************************************************************************
eststo clear

xi: ivreg2 learn (served=drawlo) $somecovariates, cluster(pnr)
est store iv
quietly {
	reg served drawlo $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):iv
		}
foreach Y of num 1/4 {
xi: ivreg2 learn (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store iv`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':iv`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):iv`Y'
	}
}
esttab iv iv1 iv2 iv3 iv4 using "taba13.txt", tex replace ///
scalar ("N_g N. clusters" "F_first First stage F-test") ///
nomtitles se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 





*Tab a15 draft status, service status and mising earnings
*Tab a16 missing earnings by age and AFQT score quartile

set more 1
prog drop _all

use pnr qafqt drawlo served lotterydraw learn age etdisp meduc feduc yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane birthweight using "opp-cost-master", clear
*keep if learn!=.
keep if age >24
egen posearn=count(learn), by(pnr)
replace learn=posearn==0
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
keep if unikpnr==1
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"


global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"



label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000
*for X in any height afqt: for Y in num 1/2: replace XsqY=XsqY/100
********************************************************************************



prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end



********************************************************************************
*reduced form
eststo clear
forval y=0/4 {
  reg learn drawlo ${cov`y'}, cluster(pnr) 
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab_missearn_rf.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

*ols
set more 1
eststo clear
forval y=0/4 {
  reg learn served ${cov`y'}, cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab_missearn_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(served) obslast

*first stage
set more 1
eststo clear
forval y=0/4 {
  reg served drawlo ${cov`y'}, cluster (pnr)
  gen mn=served
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab_missearn_fs.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tab_missearn_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast


* by AFQT quartile
* instrumental variables
eststo clear
forval y=1/4 {
  gen mn=learn if qafqt==`y'
  ivreg2 learn (served=drawlo) ${cov3} if qafqt==`y', first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i1 i2 i3 i4 using "tab_missearn_qafqt.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("Lowest" "Q2" "Q3" "Highest") /// 
label keep(served) obslast



prog define byage
use if age==`1' using "opp-cost-master", clear
replace learn=learn==.
eststo clear
*gen mn=learn
*ivreg2 learn (served=drawlo) ${cov3}, first ffirst cluster (pnr)
*ivkeep 0
forval y=1/4 {
  gen mn=learn if qafqt==`y'
  ivreg2 learn (served=drawlo) ${cov3} if qafqt==`y', first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i1 i2 i3 i4 using "tab_missearn_qafqt_age`1'.txt", tex se replace starlevels(* 0.10 ** 0.05 *** 0.010) mtitles ("Lowest" "Q2" "Q3" "Highest") label keep(served) obslast
end

for A in num 25/35: byage A




*Table A17 - excluding low earnings


prog drop _all

use pnr learn age etdisp lotterydraw served drawlo yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12
capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

drop if learn<7.228096
save templow1, replace
drop if learn<10.21234
save templow5, replace
drop if learn<11.06257
save templow10, replace

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000
*for X in any height afqt: for Y in num 1/2: replace XsqY=XsqY/100
********************************************************************************

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end


********************************************************************************

prog define droplowearn
* instrumental variables
use templow`1', clear
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba17_droplow`1'_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast
end

for D in num 1 5 10: droplowearn D



*Table A18 - effects of military service on experience


prog drop _all

use pnr days age learn etdisp drawlo served lotterydraw yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear
recode days .=0 365/max=365
gen ftfy=days/365
keep if age>=18
sort pnr age
qby pnr: gen ftfys18=sum(ftfy) if age>=18
qby pnr: gen ftfys25=sum(ftfy) if age>=25

replace learn=ftfys18


*keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000
*for X in any height afqt: for Y in num 1/2: replace XsqY=XsqY/100
********************************************************************************



prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end


********************************************************************************
*reduced form
eststo clear
forval y=0/4 {
  reg learn drawlo ${cov`y'}, cluster(pnr) 
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba18_main_rf.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

*ols
set more 1
eststo clear
forval y=0/4 {
  reg learn served ${cov`y'}, cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba18_main_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(served) obslast

*first stage
set more 1
eststo clear
forval y=0/4 {
  reg served drawlo ${cov`y'}, cluster (pnr)
  gen mn=served
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba18_main_fs.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba18_main_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast





*Table a19 - experience by AFQT score quartile


prog drop _all

use qafqt pnr days age learn etdisp drawlo served lotterydraw yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear

recode days .=0 365/max=365
gen ftfy=days/365
keep if age>=18
sort pnr age
qby pnr: gen ftfys18=sum(ftfy) if age>=18
qby pnr: gen ftfys25=sum(ftfy) if age>=25

replace learn=ftfys18


*keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"


label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000
*for X in any height afqt: for Y in num 1/2: replace XsqY=XsqY/100
********************************************************************************



prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end


********************************************************************************

*ols
set more 1
eststo clear
forval y=1/4 {
  reg learn served ${cov2} if qafqt==`y', cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i1 i2 i3 i4 using "taba19_q_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
label keep(served) obslast


* instrumental variables
set more 1
eststo clear
forval y=1/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov2} if qafqt==`y', first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i1 i2 i3 i4 using "taba19_q_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("1st quartile" "2nd quartile" "3rd quartile" "Top quartile") /// 
label keep(served) obslast





*TABLE A20 - EFFECTS OF MILITARY SERVICE ON HEALTH OUTCOMES BY HEIGHT QUARTILE
* AND BIRTH WEIGHT QUARTILE

use "opp-cost-master", clear

keep if learn!=.
keep if age >24
replace meduc=meduc/12
replace feduc=feduc/12

duplicates drop pnr, force

xtile qbw=birthweight, n(4)
xtile qheight=height, n(4)


for Y in any before after:replace Yunemp=Yunemp/1000

global somecovariates "i.yob i.mob i.shy i.sessan_year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"
********************************************************************************
* BIRTH WEIGHT
* Panel D. Hospitalization=1 for age 26-35
eststo clear
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforelpr $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates if qbw==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforelpr $somecovariates if qbw==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelDbybirthweight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* Panel E. Prescription medicine=1 for age 26-35
eststo clear
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforemedicine $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates if qbw==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforemedicine $somecovariates if qbw==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelEbybirthweight.txt".txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* Panel F. Psychiatric diagnosis=1 for age 26-35
eststo clear
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforepsykdiag $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates if qbw==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforepsykdiag $somecovariates if qbw==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelFbybirthweight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
    
  
* Panel G. Addiction treatment=1 for age 26-35
eststo clear
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforeaddiction $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates if qbw==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforeaddiction $somecovariates if qbw==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelGbybirthweight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
      
********************************************************************************
*BY HEIGHT
set more 1
* BIRTH WEIGHT
*Panel D.Hospitalization=1 for age 26-35
eststo clear
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforelpr $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterlpr (served=drawlo) beforelpr $somecovariates if qheight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforelpr $somecovariates if qheight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelDbyheight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
  

* Panel E. Prescription medicine=1 for age 26-35
eststo clear
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforemedicine $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 aftermedicine (served=drawlo) beforemedicine $somecovariates if qheight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforemedicine $somecovariates if qheight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelEbyheight.txt".txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile")///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
  
* Panel F. Psychiatric diagnosis=1 for age 26-35
eststo clear
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforepsykdiag $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afterpsykdiag (served=drawlo) beforepsykdiag $somecovariates if qheight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforepsykdiag $somecovariates if qheight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelFbyheight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
    
* Panel G. Addiction treatment=1 for age 26-35
eststo clear
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates, cluster(pnr)
est store full
quietly {
	reg served drawlo beforeaddiction $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':full
		}

foreach Y of num 1/4 {
xi: ivreg2 afteraddiction (served=drawlo) beforeaddiction $somecovariates if qheight==`Y', cluster(pnr)
est store Q`Y'
quietly {
	reg served drawlo beforeaddiction $somecovariates if qheight==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':Q`Y'
		}
}
esttab full Q1 Q2 Q3 Q4  using "taba20panelGbyheight.txt", tex ///
replace scalar ("F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)
      



*Tab A.4 Effect of military service on earnings (excluding taxable benefits) ///
* across quartiles of AFQT scores

set more 1
use "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

drop unikpnr
by pnr, sort : gen unikpnr=_n

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"

gen lkearn=log(kearn)
keep if lkearn!=.
********************************************************************************
eststo clear
xi: ivreg2 lkearn (served=drawlo) $somecovariates, cluster(pnr)
est store ivs
quietly {
	reg served drawlo $somecovariates, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':ivs
		}


foreach Y of num 1/4 {
xi: ivreg2 lkearn (served=drawlo) $somecovariates if qafqt==`Y', cluster(pnr)
est store ivs`Y'
quietly {
	reg served drawlo $somecovariates if qafqt==`Y', cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':ivs`Y'
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):ivs`Y'
	}
}
esttab ivs ivs1 ivs2 ivs3 ivs4 using "tabA4.txt", ///
tex replace scalar ("N_g N. clusters" "F_first First stage F-test") ///
mtitles ("Full sample" "1st quartile" "2nd quartile" "3rd quartile" "Top quartile") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served) 




*Table A5 - effect of military service on discounted earnings ages 25-30


prog drop _all

use pnr age served drawlo lotterydraw learn etdisp yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear
keep if learn!=.
keep if age >24 & age<31
for R in num 0 3 5: gen dvR=0
for R in num 0 3 5: for A in num 25/30: replace dvR=exp(learn)/(1.0R^(A-24)) if age==A

gsort pnr -age
for R in num 0 3 5: qby pnr: gen pdvR=sum(dvR)
keep if age==25

gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end



prog define discounted
replace learn=log(pdv`1')
*ols
set more 1
eststo clear
forval y=0/4 {
  reg learn served ${cov`y'}, cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba5_pdv`1'_ols.txt", tex se replace scalar ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(served) obslast
* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba5_pdv`1'_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast
end


for R in num 0 3 5: discounted R






*Tab A6 - draft status, military status and earnings--excluding 4 months service

use learn age etdisp pnr yob mob shy sessan_year year height afqt height2 afqt2 ///
fromsingleparentfamily outofhomecare dane meduc feduc birthweight drawlo served ///
lotterydraw using "opp-cost-master", clear

prog drop _all

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

drop if spaer_year>2006

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"

label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

*reduced form
eststo clear
forval y=0/4 {
  reg learn drawlo ${cov`y'}, cluster(pnr) 
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tabA6rf.txt", tex se replace scalar ///
("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") ///
starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

*ols
set more 1
eststo clear
forval y=0/4 {
  reg learn served ${cov`y'}, cluster (pnr)
  gen mn=learn
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tabA6ols.txt", tex se replace scalar ///
("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") ///
starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(served) obslast

*first stage
set more 1
eststo clear
forval y=0/4 {
  reg served drawlo ${cov`y'}, cluster (pnr)
  gen mn=served
  olskeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tabA6fs.txt", tex se replace scalar ///
 ("ar2 Adjusted R2" "m mean of dep var" "N_g Individuals") ///
 starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") ///
label keep(drawlo) obslast

* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "tabA6iv.txt", tex se replace scalar ///
 ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") ///
 starlevels(* 0.10 ** 0.05 *** 0.010) ///
 mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast





*Table A7 - excluding men close to draft threshold


prog drop _all

use pnr spaer_year spaer_month etdisp learn age served drawlo lotterydraw yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane meduc feduc birthweight using "opp-cost-master", clear

gen th1=spaer_year
for Y in num 1994/2007 \ T in num 21000 21000 25000 25000 24000 24000 8000 18000 26000 36000 31000 27000 18000 15000: recode th1 Y=T
gen th2=spaer_year
for Y in num 1994/2007 \ T in num 9000 9000 12000 15000 13000 15000 3000 15000 16000 12000 8000 8000 4000 1600: recode th2 Y=T
gen threshold=th1*(spaer_month<=6)+th2*(spaer_month>=7)

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12
capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1
for D in num 360 1800 3600: drop if abs(lotterydraw-threshold)<D \ save tempD, replace


global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000



prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end



prog define closetothreshold
use temp`1', clear
* instrumental variables
set more 1
eststo clear
forval y=0/4 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov`y'}, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 using "taba7_prox`1'_iv.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("No controls" "Basic" "Extended I" "Extended II" "Extended III") /// 
label keep(served) obslast
end

for D in num 360 1800 3600: closetothreshold D






* Table A.8 Effect of military service on earnings for various subgroups
 

use "opp-cost-master", clear

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

drop unikpnr
by pnr, sort : gen unikpnr=_n

global somecovariates "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"

label var drawlo "Draft status=1"
label var served "Service status=1"
********************************************************************************
* IV
* (1) Son of immigrant
eststo clear
xi: ivreg2 learn (served=drawlo)$somecovariates if immigrant==1, cluster(pnr)
est store immigrant
quietly {
	reg served drawlo $somecovariates if immigrant==1, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':immigrant
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):immigrant
}

* (2)Single-parent family
xi: ivreg2 learn (served=drawlo)$somecovariates if fromsingleparentfamily==1 , cluster(pnr)
est store singpar
quietly {
	reg served drawlo $somecovariates if fromsingleparentfamily==1, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':singpar
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):singpar
}





* (3)Out-of-home care 
xi: ivreg2 learn (served=drawlo)$somecovariates if outofhomecare==1, cluster(pnr)
est store care
quietly {
	reg served drawlo $somecovariates if outofhomecare==1, cluster(pnr)
	test drawlo
	local F_temp=r(F)
	estadd scalar F_first=`F_temp':care
	count if e(sample)& unikpnr==1
	estadd scalar N_g=r(N):care
}


esttab immigrant care singpar using "tabaA8iv.txt", tex ///
replace scalar("N_g N. clusters" "F_first First stage F-test" "coef Drafted=1 from 1ststage") ///
mtitle ("Son of immigrant" "Out-of-home care family" "Single-parent family") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)


* First Stage
eststo clear
reg served drawlo $somecovariates if immigrant==1, cluster(pnr)
est store fsimmigrant
reg served drawlo $somecovariates if fromsingleparentfamily==1, cluster(pnr)
est store fssingpar
reg served drawlo $somecovariates if outofhomecare==1, cluster(pnr)
est store fscare
esttab fsimmigrant using "taba8fsimmigrant.txt", tex replace ///
mtitle ("Son of immigrant")se star(* 0.10 ** 0.05 *** 0.010)label keep(drawlo)
esttab fssingpar using "taba8fssingle-parent-family.txt", tex replace mtitle ///
 ("single-parent family")se star(* 0.10 ** 0.05 *** 0.010)label keep(drawlo)
esttab fscare using "taba8fsout-of-home-care.txt", tex replace ///
mtitle ("Out-of-home care")se star(* 0.10 ** 0.05 *** 0.010)label keep(drawlo)



*OLS
* (1)Son of immigrant
set more 1
eststo clear
reg learn served $somecovariates if immigrant==1, cluster(pnr)robust
est store immigrant
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):immigrant

esttab immigrant using "taba8ols-son-of-immigrant.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Son of immigrant") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* (2) Out-of-home care
set more 1
eststo clear
reg learn served $somecovariates if outofhomecare==1, cluster(pnr) robust
est store care
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):care

esttab care using "taba8ols-out-of-home-care.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Out-of-home care") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

* (3) Single-parent family
set more 1
eststo clear
reg learn served $somecovariates if fromsingleparentfamily==1 , cluster(pnr)robust
est store singpar
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):singpar

esttab singpar using "taba8ols-single-parent-family.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Single-parent family") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(served)

*Reduced Form
* (1) Son of immigrant
set more 1
eststo clear
reg learn drawlo $somecovariates if immigrant==1, cluster(pnr)robust
est store immigrant
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):immigrant

esttab immigrant using "taba8rf-son-of-immigrant.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Son of immigrant") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(drawlo)

* (2) Single- parent family
reg learn drawlo $somecovariates if fromsingleparentfamily==1 , cluster(pnr)robust
est store singpar
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):singpar

esttab singpar using "tab8rf-single-parent-family.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Single-parent family") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(drawlo)

* (3) Out-of-home care
reg learn drawlo $somecovariates if outofhomecare==1, cluster(pnr) robust
est store care
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):care

esttab care using "taba8rf-out-of-home-care.txt", tex ///
replace ar2 scalar("N_g N. clusters") mtitle ("Out-of-home care") ///
se star(* 0.10 ** 0.05 *** 0.010) label keep(drawlo)



*Tab a9 - effect of military service on earnings by observation year

use pnr qafqt drawlo served lotterydraw learn age etdisp meduc feduc yob mob shy sessan_year year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane birthweight using "opp-cost-master", clear
prog drop _all

keep if learn!=.
keep if age >24
gen letdisp=log(etdisp)
replace meduc=meduc/12
replace feduc=feduc/12

capture drop unikpnr
by pnr, sort : gen unikpnr=_n
gen nocov=1

global cov0 "nocov"
global cov1 "i.yob i.mob i.shy i.sessan_year i.year"
global cov2 "i.yob i.mob i.shy i.sessan_year i.year height2 afqt2 height afqt"
global cov3 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane" 
global cov4 "i.yob i.mob i.shy i.sessan_year i.year height afqt height2 afqt2 fromsingleparentfamily outofhomecare dane letdisp meduc feduc birthweight"
global cova "i.yob i.mob i.shy i.sessan_year i.year"
global covb "height afqt fromsingleparentfamily outofhomecare dane"
global covc "letdisp meduc feduc birthweight"

label var height "height"
label var afqt "AFQT"
label var fromsingleparentfamily "raised by single parent"
label var outofhomecare "placed in out-of-home care"


label var drawlo "Draft status=1"
label var served "Service status=1"
replace lotterydraw=lotterydraw/36000


prog define olskeep
est store i`1'
scalar ar2=e(r2_a)
estadd scalar ar2=ar2:i`1'
su mn if e(sample)
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end

prog define ivkeep
est store i`1'
mat first=e(first)
mat f=first["SWF",1]
scalar f=f[1,1]
estadd scalar f=f:i`1'
su mn if e(sample)
scalar m=r(mean)
drop mn
estadd scalar m=m:i`1'
count if e(sample)& unikpnr==1
estadd scalar N_g=r(N):i`1'
end




* instrumental variables by chohort for ALL
set more 1
eststo clear
forval y=0/7 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov3} if year>=(2000+`y') & year<=(2004+`y'), first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 i5 i6 i7 using "tab_earn_by_5yc_all.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("2000-4" "2001-5" "2002-6" "2003-7" "2004-8" "2005-9" "2006-10" "2007-11") /// 
label keep(served) obslast

* instrumental variables by cohort for AFQTQ4
set more 1
eststo clear
forval y=0/7 {
  gen mn=learn
  ivreg2 learn (served=drawlo) ${cov3} if year>=(2000+`y') & year<=(2004+`y') & qafqt==4, first ffirst cluster (pnr)
  ivkeep `y'
  }
esttab i0 i1 i2 i3 i4 i5 i6 i7 using "tab_earn_by_5yc_afqtq4.txt", tex se replace scalar ("f F-stat excl. inst." "m mean of dep var" "N_g Individuals") starlevels(* 0.10 ** 0.05 *** 0.010) ///
mtitles ("2000-4" "2001-5" "2002-6" "2003-7" "2004-8" "2005-9" "2006-10" "2007-11") /// 
label keep(served) obslast












     	