cd "${rawdata_path}"

*Pull variables from raw data, rename variables, and generate variables

*1 Retirement dates
use "pkal.dta", clear

keep pid hid syear kal1e01 kal1e02
rename kal1e01 retired
rename kal1e02 retired_months

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

*2 Demographics
use "ppathl.dta", clear
keep pid hid syear gebjahr gebmonat sex parid loc1989 psample migback

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

*3 Parent's IDs and IDs of the partner's parents
use "bioparen.dta", clear

keep persnr fnr mnr
rename fnr vnr
rename persnr pid
save "${output_path}\tmp\IDs_parents.dta", replace

rename pid parid
rename vnr vilnr
rename mnr milnr
save "${output_path}\tmp\IDs_inlaws.dta", replace

*4 Other individual and household variables
use "pequiv.dta", clear
keep pid syear h11101
rename h11101 children
save "${output_path}\tmp\pequiv.dta", replace

use "hbrutto.dta", clear
keep hid syear regtyp

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


use "hgen.dta", clear
keep hid syear hghinc

mvdecode hghinc, mv(-9/-1)
save "${output_path}\tmp\hgen.dta", replace

use "pl.dta", clear
rename pmonin month

*Distance to father/mother - coding varies across the survey years
gen father_near=.
replace father_near=1 if syear==1991 								& pld0024==1 & (plj0119_v1>=1 & plj0119_v1<=4)
replace father_near=1 if (syear==1996 | syear==2001) 				& pld0024==1 & (plj0119_v2>=1 & plj0119_v2<=4)
replace father_near=1 if (syear==2006 | syear==2011 | syear==2016) & pld0024==1 & (plj0119_v3>=0 & plj0119_v3<=4)
replace father_near=0 if syear==1991 								& pld0024==1 & (plj0119_v1==5 | plj0119_v1==6)
replace father_near=0 if (syear==1996 | syear==2001) 				& pld0024==1 & (plj0119_v2==5 | plj0119_v2==6)
replace father_near=0 if (syear==2006 | syear==2011 | syear==2016) & pld0024==1 & (plj0119_v3==5 | plj0119_v3==6)
replace father_near=0 if 											pld0024==-2

gen mother_near=.
replace mother_near=1 if syear==1991 								& pld0023==1 & (plj0118_v1>=1 & plj0118_v1<=4)
replace mother_near=1 if (syear==1996 | syear==2001) 				& pld0023==1 & (plj0118_v2>=1 & plj0118_v2<=4)
replace mother_near=1 if (syear==2006 | syear==2011 | syear==2016) & pld0023==1 & (plj0118_v3>=0 & plj0118_v3<=4)
replace mother_near=0 if syear==1991 								& pld0023==1 & (plj0118_v1==5 | plj0118_v1==6)
replace mother_near=0 if (syear==1996 | syear==2001) 				& pld0023==1 & (plj0118_v2==5 | plj0118_v2==6)
replace mother_near=0 if (syear==2006 | syear==2011 | syear==2016) & pld0023==1 & (plj0118_v3==5 | plj0118_v3==6)
replace mother_near=0 if 											pld0023==-2

*Time use
rename pli0022_h time_childcare_sun 
rename pli0019_h time_childcare_sat
rename pli0044_h time_childcare
rename pli0038_h time_job

rename plh0007 politics
rename plc0232 pension
recode pension (-2=0)
rename plc0223_h pension_amount

rename plb0439_v2 retirement_expectation

rename plh0258_h religion
gen chris_rel=cond(religion==1 | religion==2 | religion==3 | religion==7,1, cond((religion>=4 & religion<=6) | (religion>=8 & religion<=11), 0,.))

keep pid hid syear month father_near mother_near time_childcare time_childcare_sat time_childcare_sun politics time_job politics pension pension_amount chris_rel retirement_expectation

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

use pgen.dta, clear
keep pid syear pgemplst pgexpft pgexppt pgexpue pgpsbil pgbilzeit
save "${output_path}\tmp\pgen.dta", replace

*Merge different datasets together
cd "${output_path}\tmp\"

merge 1:1 pid syear using pl.dta, keep(match) nogen
merge 1:1 pid syear using pequiv.dta, keep(match) nogen
merge m:1 hid syear using hgen.dta, keep(match) nogen
merge m:1 hid syear using hbrutto.dta, keep(match) nogen
merge 1:1 pid syear using ppathl.dta, keep(match) nogen
merge 1:1 pid syear using pkal.dta, keep(match) nogen
merge m:1 pid using IDs_parents.dta, keep(match master) gen(merge_parents)
merge m:1 parid using IDs_inlaws.dta, keep(match master) gen(merge_inlaws)

*Only keep observations where either parents or parents-in-law where successfully merged
drop if merge_parents!=3 & merge_inlaws!=3

*Merge birth histories
merge 1:1 pid syear using "Birth_history_all.dta", keep(match) nogen
save "long_data.dta", replace

mvdecode _all, mv(-9/-1)
drop if month==0 | month==.
*Generate age
gen gebjahr_dez=gebjahr*12-1960*12
gen gebdatum_dez=gebjahr_dez+gebmonat-1

format gebdatum_dez %tm

gen syear_dez=syear*12-1960*12
gen sdatum_dez=syear_dez+month-1

format sdatum_dez %tm

gen age_months=sdatum_dez-gebdatum_dez

gen age=age_months/12


*Generate retirement date
recode retired (2=0)

xtset pid syear
replace retired=1 if l.retired==1 & retired==0


gen ret_year=syear if retired==0 & f.retired==1
gen ret_month=13-f.retired_months if ret_year==syear


replace retired=1 if retired==1 & pgemplst==5
replace retired=1 if retired==0 & pgemplst==5 & ret_month<=month
replace retired=0 if retired==1 & pgemplst==1
replace retired=1 if l.retired==1 & retired==0

*This is the main dataset for the adult children
save "child.dta", replace

*Generate variables for fathers and mothers
preserve
keep if sex==1 
keep pid syear retired age hid loc1989 parid gebjahr gebmonat pgexpft pgexpue pgexppt time_childcare time_childcare_sat time_childcare_sun time_job month pension pension_amount pgpsbil migback pgbilzeit chris_rel retirement_expectation

rename pid vnr
foreach var of varlist retired age hid loc1989 parid gebjahr gebmonat pgexpft pgexpue pgexppt time_childcare time_childcare_sat time_childcare_sun time_job month pension pension_amount pgpsbil migback  pgbilzeit chris_rel retirement_expectation{
rename `var' v`var'
}

save "fathers.dta", replace
restore


preserve
keep if sex==2
keep pid syear retired age hid loc1989 parid gebjahr gebmonat pgexpft pgexpue pgexppt time_childcare time_childcare_sat time_childcare_sun time_job month pension  pension_amount pgpsbil migback  pgbilzeit chris_rel retirement_expectation
 
rename pid mnr
foreach var of varlist retired age hid loc1989 parid gebjahr gebmonat pgexpft pgexpue pgexppt time_childcare time_childcare_sat time_childcare_sun time_job month pension  pension_amount pgpsbil migback  pgbilzeit chris_rel retirement_expectation{
rename `var' m`var'
}

save "mothers.dta", replace
restore



mvdecode vnr mnr vilnr milnr, mv(-9/-1)

gen father_known=cond(vnr!=.,1,0)
gen mother_known=cond(mnr!=.,1,0)
gen fil_known=cond(vilnr!=.,1,0)
gen mil_known=cond(milnr!=.,1,0)

*Use info on in-laws only if biological parents are not observed in the data
replace vnr=vilnr if vnr==. & vilnr!=.
replace mnr=milnr if mnr==. & milnr!=.

*Merge parents and children
merge m:1 vnr syear using "fathers.dta", keep(master match) gen(merge_fathers)
merge m:1 mnr syear using "mothers.dta", keep(master match) gen(merge_mothers)

*Keep only observations where either info on father or mother was successfully merged, and where the partner ID of the father matches the ID of the mother and vice versa
keep if merge_fathers==3 | merge_mothers==3
tab merge_fathers merge_mothers

keep if vparid==mnr | merge_mothers!=3
keep if mparid==vnr | merge_fathers!=3

save "Linked_data.dta", replace
