clear all set mem 200M cd e:\chililabdata\data use individual sort individid save individual, replace use observation sort observeid save observation, replace use individres drop if (res_edate != . & res_edate < $refdate) drop if (res_sdate != . & res_sdate > $refdate) sort individ duplicates drop individid, force save cross\womanresidency, replace use membership drop if (mem_edate != . & mem_edate < $refdate) drop if (mem_sdate != . & mem_sdate > $refdate) gen refdate = $refdate rename mem_sobserveid observeid duplicates drop individid, force sort observeid merge observeid using observation tab _merge drop if _merge!=3 drop _merge rename locationid sociallocid sort individid merge individid using individual tab _merge drop if _merge != 3 drop _merge duplicates drop individid, force keep if gender == "2" sort individ merge individid using cross\womanresidency gen isresident = (sociallocid==locationid) gen communeid = substr(sociallocid, 1,2) drop if isresident == $isresident replace communeid = "00" if $communeid==0 drop if communeid ! ="0$communeid" gen age = floor((refdate-birthdate)/365.5) drop if age < 15 | age > 49 recode age (min/0=1 "<1") (1/4=2 "1-4") (5/9=3 "5-9")(10/14=4 "10-14") (15/19=5 "15-19") (20/24=6 "20-24") (25/29=7 "25-29") (30/34=8 "30-34") (35/39=9 "35-39") (40/44=10 "40-44") (45/49=11 "45-49")(50/54=12 "50-54") (55/59=13 "55-59") (60/64=14 "60-64") (65/69=15 "65-69") (70/74=16 "70-74") (75/79=17 "75-79") (80/max=18 ">=80") , gen (agegrp) sort communeid agegrp collapse (count) women = mem_episodeid , by (agegrp communeid refdate) save cross\WomenBeginYear, replace cd e:\chililabdata