clear all set mem 200M cd e:\chililabdata\data use individual sort individid save individual, replace use observation sort observeid save observation, replace use death gen year = year(dth_date) drop if year != $year sort observeid merge observeid using observation gen communeid = substr(locationid, 1,2) replace communeid = "00" if $communeid==0c:\chililabdata drop if _merge !=3 keep individid communeid year dth_date sort individid merge individid using individual drop if _merge !=3 save death\tmpdeaths, replace keep communeid gender individid year dth_date sort individid gen maledeaths = 0 gen femaledeaths =0 replace maledeaths = 1 if gender == "1" replace femaledeaths =1 if gender == "2" merge individid using individual drop if _merge != 3 gen age = floor((dth_date-birthdate)/365.5) 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 year communeid agegrp collapse (sum) maledeaths femaledeaths, by (year communeid agegrp) sort communeid agegrp save $outfile, replace cd e:\chililabdata