* do e:\DHS\programs\merging\merge_PR_BR_IR_MR_do_3June2019.txt ***************************************************** program define prepare_for_merge * Use the IR and MR files to attach information about the child's parents * Variables "in_PR", etc. are not needed but can be helpful in checking the merge * Prepare IR file for merge; keep the variables you want from mothers use IR.dta, clear keep v0* v1* gen in_IR=1 rename v001 hv001 rename v002 hv002 rename v003 hvidx gen mo_line=hvidx sort hv001 hv002 mo_line save IR.dta, replace * Prepare MR file for merge; keep the variables you want from fathers use MR.dta, clear keep mv0* mv1* gen in_MR=1 rename mv001 hv001 rename mv002 hv002 rename mv003 hvidx gen fa_line=hvidx sort hv001 hv002 fa_line save MR.dta, replace * Prepare BR file for merge; keep the variables you want from the children use BR.dta, clear keep if b5==1 keep v001 v002 v005 b* gen in_BR=1 rename v001 hv001 rename v002 hv002 rename b16 hvidx sort hv001 hv002 hvidx save BR.dta, replace * Prepare PR file for merge; keep the variables you want from the household use PR.dta, clear keep hv001 hv002 hvidx hv0* hv1* gen in_PR=1 sort hv001 hv002 hvidx save PR.dta, replace end ***************************************************** program define merge_files use PR.dta, clear * Merge with BR to identify the children who are living with their mother merge hv001 hv002 hvidx using BR.dta rename _merge _merge_PR_BR keep if hv105<18 tab1 in_*,m drop if in_PR==. gen mo_line=hv112 gen fa_line=hv114 gen in_PR_BR=1 sort hv001 hv002 mo_line save PR_BR.dta, replace * Use the IR and MR files to attach information about the child's parents; this * information is only available if the parents are in the same household as the child * Merge with IR to get the mother merge hv001 hv002 mo_line using IR.dta rename _merge _merge_PR_BR_IR tab1 in_*,m sort hv001 hv002 fa_line * Merge with MR to get the father merge hv001 hv002 fa_line using MR.dta rename _merge _merge_PR_BR_IR_MR keep if in_PR==1 tab1 in_*,m save merged_file.dta, replace end ***************************************************** ***************************************************** ***************************************************** ***************************************************** ***************************************************** * EXECUTION BEGINS HERE set more off set maxvar 10000 * You need to specify a default folder where the files will be kept cd e:\DHS\DHS_data\scratch * Read data files and save with generic file names use "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\COPR72FL.DTA" , clear save PR.dta, replace use "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\COBR72FL.DTA" , clear save BR.dta, replace use "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\COIR72FL.DTA" , clear save IR.dta, replace use "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\COMR71FL.DTA" , clear save MR.dta, replace prepare_for_merge merge_files