clear cd e:\chililabdata\data use individual sort individid save, replace use pregnance sort individid merge m:1 individid using individual, keepusing(birthdate) drop if _merge!=3 drop _merge gen socialgpid = substr(sobserveid,1,9) replace preg_sdate= preg_sdate/86400000 rename preg_sdate sdate replace preg_edate= preg_edate/86400000 rename preg_edate edate replace birthdate= birthdate/86400000 rename birthdate bod format bod %td format sdate %td format edate %td gen year = year( sdate) gen h = halfyear(sdate) gen round = yh(year,h)-89 tostring round, generate(rounds) replace rounds = "0" + rounds if strlen(rounds) ==1 gen socialkey = socialgpid+ rounds gen individkey = individid+ rounds drop eventid sobserveid fatherid fatheridGuid EID rounds h year sort individid round drop if round < 0 save rPregnance, replace