*This file generates a birth history with variables for the year and the month in which a birth occured.
set more off
cd "${rawdata_path}"

use "biobirth.dta", clear

keep persnr sumkids kidgeb* kidmon*
rename persnr pid

mvdecode kidgeb*, mv(-2)

foreach num of numlist 1984/2017{
gen birth`num'=0
gen birthmonth`num'=.

replace birth`num'=1 if kidgeb01==`num'
replace birthmonth`num'=kidmon01 if kidgeb01==`num'
replace birth`num'=1 if kidgeb02==`num'
replace birthmonth`num'=kidmon02 if kidgeb02==`num'
replace birth`num'=1 if kidgeb03==`num'
replace birthmonth`num'=kidmon03 if kidgeb03==`num'
replace birth`num'=1 if kidgeb04==`num'
replace birthmonth`num'=kidmon04 if kidgeb04==`num'
replace birth`num'=1 if kidgeb05==`num'
replace birthmonth`num'=kidmon05 if kidgeb05==`num'
replace birth`num'=1 if kidgeb06==`num'
replace birthmonth`num'=kidmon06 if kidgeb06==`num'
replace birth`num'=1 if kidgeb07==`num'
replace birthmonth`num'=kidmon07 if kidgeb07==`num'
replace birth`num'=1 if kidgeb08==`num'
replace birthmonth`num'=kidmon08 if kidgeb08==`num'
replace birth`num'=1 if kidgeb09==`num'
replace birthmonth`num'=kidmon09 if kidgeb09==`num'
replace birth`num'=1 if kidgeb10==`num'
replace birthmonth`num'=kidmon10 if kidgeb10==`num'
replace birth`num'=1 if kidgeb11==`num'
replace birthmonth`num'=kidmon11 if kidgeb11==`num'
replace birth`num'=1 if kidgeb12==`num'
replace birthmonth`num'=kidmon12 if kidgeb12==`num'
replace birth`num'=1 if kidgeb13==`num'
replace birthmonth`num'=kidmon13 if kidgeb13==`num'
replace birth`num'=1 if kidgeb14==`num'
replace birthmonth`num'=kidmon14 if kidgeb14==`num'
replace birth`num'=1 if kidgeb15==`num'
replace birthmonth`num'=kidmon15 if kidgeb15==`num'
}

reshape long birth birthmonth, i(pid) j(syear)


save "${output_path}\tmp\Birth_history_all.dta", replace
