* Program to construct pairs of children, a reference child plus next earlier sibling * This example does not include the mother's variables and only includes bidx and b5 for the child cd e:\DHS\DHS_data\scratch use v001 v002 v003 bidx b5 using "C:\Users\26216\ICF\Analysis - Shared Resources\Data\DHSdata\NGBR7AFL.DTA", clear *keep v001 v002 v003 bidx b5 egen sibsize=count(bidx),by(v001 v002 v003) * construct two files, one for the younger child in a pair and one for the older child sort v001 v002 v003 bidx save temp_younger.dta, replace rename b* b*_sib * attach the child with bidx=2 to the child with bidx=1 * attach the child with bidx=3 to the child with bidx=2 * etc. gen bidx=bidx_sib-1 sort v001 v002 v003 bidx merge v001 v002 v003 bidx using temp_younger.dta tab _merge * the child with bidx=1 does not get attached to another child * the child with bidx=sibsize does not have another child attached to him/her * remove these two types of cases drop if bidx==0 | bidx_sib==. tab _merge drop _merge order v001 v002 v003 bidx b5 bidx_sib b5_sib list if _n<=50, table clean