clear all set mem 1000M cd e:\chililabdata\data use individual sort individid replace motherid = trim(motherid) drop if strlen(motherid) < 11 sort motherid egen maxchild = count(birthdate), by (motherid) drop if maxchild < 2 sort motherid birthdate by motherid: gen no=_n tostring no, gen(no1) gen key = motherid + no1 drop no1 sort key save person1, replace replace no=no +1 tostring no, gen(no1) replace key = motherid + no1 keep individid birthdate gender key rename individid prevchildid rename birthdate prevbirthdate rename gender prevgender sort key save person2, replace brow use person1 merge key using person2 keep if _merge ==3 keep individid prevchildid prevbirthdate prevgender sort individid duplicates drop individid, force save personRel, replace use birth\allbirths sort individid merge individid using personRel keep if _merge!=2 save, replace cd e:\chililabdata