clear all set memo 200M cd e:\chililabdata\data\cross global sourcename = "E:\chililabdata\data\cross2012.dta" global savefolder ="e:\" global isresident=1 use $sourcename drop if isresident == $isresident gen communeid = substr(sociallocid,1,2) gen area=1 replace area=2 if (communeid=="02") replace area=2 if (communeid=="04") replace area=2 if (communeid=="05") replace area=2 if (communeid=="07") gen age = floor((refdate-bod)/365.5) gen male =0 gen female =0 replace male =1 if gender == "1" replace female =1 if gender == "2" keep area age communeid male female refdate sort age 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) save $savefolder\popyramidfull$suffix, replace sort communeid agegrp collapse (sum) male female, by (communeid agegrp refdate ) save $savefolder\popyramidby_commune_agegrp$suffix, replace use $savefolder\popyramidfull$suffix, clear save $savefolder\popyramidby_area_agegrp$suffix, replace collapse (sum) male female, by (area agegrp refdate ) collapse (sum) male female, by (agegrp refdate) egen totalmale =sum(male) egen totalfemale= sum(female) gen totalage = totalmale + totalfemale gen pmale = -100*male/totalage gen pfemale = 100*female/totalage global hyphen = "/" global refday = day(refdate) global refmonth = month(refdate) global refyear = year(refdate) twoway bar pmale agegrp, horizontal xvarlab(Males)|| bar pfemale agegrp, horizontal xvarlab(Females) || , ylabel(1(1)17, angle(horizontal) valuelabel labsize(*.8)) xtitle("Population by thousands") ytitle("Age group") xlabel(-10 "10" -7.5 "7.5" -5 "5" -2.5 "2.5" 2.5 5 7.5 10) legend(label(1 Males) label(2 Females)) title("Chililab Male and Female Population by Age") subtitle($refday$hyphen$refmonth$hyphen$refyear) note("Source: Chililab DSS Database", span) save $savefolder\popyramidby_agegrp$suffix, replace cd e:\chililabdata