* this code assumes that one is already in the directory data_generation/datasources/Population_USCensusBureau

tempfile IDB
unzipfile IDB_DataSet.zip // this is the June 2012 version used in the paper. A December 2013 update is available at http://www.census.gov/population/international/data/idb/include/IDB_DataSet.zip

* population
import delimited IDBext001.txt, delimiters("|") varnames(nonames) clear
rename (v1 v2 v3) (cty year population)
label var population "mid-year population (Census IDB 6/2012)"
save `IDB', replace

* teen fertility
import delimited IDBext028.txt, delimiters("|") varnames(nonames) clear
keep v1-v3
rename (v1 v2 v3) (cty year teenfertility)
merge 1:1 cty year using `IDB', nogenerate
save `IDB', replace

* young males
import delimited IDBext094.txt, delimiters("|") varnames(nonames) clear
keep if v3=="*" | (v3=="A" & v5=="-" & inlist(v6,19,24,29))
drop v3 v4 v5 v9
rename (v1 v2 v6 v7 v8) (cty year endage bothsexes male)
reshape wide bothsexes male, i(cty year) j(endage)
foreach age in 19 24 29 {
	gen ms`=`age'-4'`age'_pop = male`age'/bothsexes0
	label var ms`=`age'-4'`age'_pop "Males `=`age'-4'-`age' / total population (US Census IDB 6/2012)"
}
drop male* bothsexes*
merge 1:1 cty year using `IDB', nogenerate
drop if !inrange(year,1950,2012)
merge m:1 cty using Census_codes.dta, nogenerate keepusing(ISO*) keep(1 3)
drop cty
order ISO year

* cleanup: merging Guernsey & Jersey into CHI; merging Gaza & West Bank into PSE
preserve
keep if inlist(ISO,"CHI","PSE")
collapse (mean) ms* teen (rawsum) pop [aw=pop], by(ISO year)
tempfile CHIPSE
save `CHIPSE'
restore
drop if inlist(ISO,"CHI","PSE")
append using `CHIPSE'
save `IDB', replace

* US fertility data from CPS
import excel USfertility_SupFertTab1.xlsx, clear cellrange(A35:D45) // this is the June 2013 version. A newer version is available at http://www.census.gov/hhes/fertility/files/cps/historical/H1.xlsx
drop B C
rename (A D) (year teenfertility)
destring year, ignore(".") replace
label var teenfertility "Fertility of 15-19 year olds (US Census IDB 6/2012; CPS for US)"
gen ISO3code="USA"
merge 1:1 ISO year using `IDB', assert(2 3) nogenerate

order ISO year
compress
