%let name=age_analysis; filename odsout '.'; /* Imitating graph from: http://healthintelligence.drupalgardens.com/content/which-percentage-people-older-and-younger-you http://flowingdata.com/2016/05/10/who-is-older-and-younger-than-you/ Using World Population Projection (link in article). http://esa.un.org/unpd/wpp/DVD/Files/1_Indicators%20(Standard)/ASCII_FILES/WPP2015_INT_F3_Population_Annual_Single_Medium.zip */ libname robsdata 'd:\Public\WPP\'; /* Using guessingrows=MAX to get the right lengths would take a *long* time. Therefore I ran Proc Import with a small guessingrows, and then copied the code out of the log, and hard-coded a longer length for the */ /* PROC IMPORT OUT=temp_data DATAFILE="d:\Public\WPP\WPP2015_INT_F3_Population_Annual_Single_Medium.csv" DBMS=CSV REPLACE; GETNAMES=YES; DATAROW=2; guessingrows=1000; run; */ /* data temp_data; infile 'd:\Public\WPP\WPP2015_INT_F3_Population_Annual_Single_Medium.csv' delimiter=',' MISSOVER DSD lrecl=32767 firstobs=2; informat LocID best32.; informat Location $100.; informat VarID best32.; informat Variant $8.; informat Time best32.; informat SexID best32.; informat Sex $8.; informat AgeGrp best12.; informat AgeGrpStart best32.; informat AgeGrpSpan best32.; informat Value best32.; format LocID best12.; format Location $100.; format VarID best12.; format Variant $8.; format Time best12.; format SexID best12.; format Sex $8.; format AgeGrp best12.; format AgeGrpStart best12.; format AgeGrpSpan best12.; format Value best12.; input LocID Location $ VarID Variant $ Time SexID Sex $ AgeGrp AgeGrpStart AgeGrpSpan Value ; run; data robsdata.WPP_data; set temp_data; run; */ %macro do_graph(yr,pngname,loc); proc sql; create table my_data as select *, value*1000 as people from robsdata.WPP_data where location="&loc" and sex="Both" and time=&yr; quit; run; proc sql noprint; select sum(people) into :total from my_data; quit; run; proc sort data=my_data out=my_data; by AgeGrp; run; data my_data; set my_data; format percent_younger percent_older percent7.1; cumulative+people; percent_younger=(cumulative-people)/&total; percent_same=people/&total; percent_older=1.00-(percent_same+percent_younger); run; data plot_data (keep = AgeGrp bar_segment value my_html); set my_data; format value percent7.1; length my_html $300; my_html='title='||quote( 'If you are '||trim(left(AgeGrp))||' years old ...'||'0d'x|| '------------------------------'||'0d'x|| trim(left(put(percent_older,percent7.1)))||' are older than you.'||'0d'x|| trim(left(put(percent_same,percent7.1)))||' are the same age as you.'||'0d'x|| trim(left(put(percent_younger,percent7.1)))||' are younger than you.' ); bar_segment='Younger'; value=percent_younger*-1; output; /* make these values negative, to print under the axis */ bar_segment='Older'; value=percent_older; output; run; data anno_age_axis; length function $8 color $12; xsys='2'; ysys='1'; hsys='3'; when='a'; do age=0 to 100 by 10; x=age; y=0; function='label'; position='8'; size=.; color=''; text=trim(left(age)); output; x=age; y=0; function='move'; output; x=age; y=100; function='draw'; color='graydd'; size=.001; when='b'; output; end; run; data anno_labels; length function $8 color $12 text $100; xsys='1'; ysys='1'; hsys='3'; when='a'; function='label'; color='gray77'; position='5'; size=3.0; x=20; y=59; text='% of population older than you'; output; x=71; y=35; text='% of population younger than you'; output; run; axis1 label=none order=(-1 to 1 by .2) style=0 major=none minor=none offset=(0,0) value=(t=1 '100%' t=2 '80%' t=3 '60%' t=4 '40%' t=5 '20%' t=6 '0%'); axis2 label=(' ' j=c 'Your Age') value=none style=0; title1 ls=1.5 "Age Distribution in &loc "; title2 a=-90 h=1 ' '; footnote link='http://esa.un.org/unpd/wpp/DVD/Files/1_Indicators%20(Standard)/ASCII_FILES/WPP2015_INT_F3_Population_Annual_Single_Medium.zip' c=gray "Data source: World Population Prospects. DESA, Population Division, UN"; pattern1 v=s c=cxb2df8a; pattern2 v=s c=cxfdbf6f; ods html anchor="&pngname"; proc gchart data=plot_data anno=anno_age_axis; format value percent7.0; note move=(85,95) height=3.0 c=gray77 "Year &yr"; vbar AgeGrp / discrete type=sum sumvar=value subgroup=bar_segment nolegend raxis=axis1 maxis=axis2 noframe autoref clipref cref=graydd coutline=grayee space=0 anno=anno_labels html=my_html des='' name="&name._&pngname"; run; %mend; ODS LISTING CLOSE; ODS html path=odsout body="&name..htm" (title="Age Distribution Graphs") style=htmlblue; goptions gunit=pct ftitle='albany amt/bold' ftext='albany amt' htitle=3.5 htext=2.2; goptions ctext=gray33; %do_graph(2016,us,United States of America); %do_graph(2016,world,WORLD); %do_graph(2016,japan,Japan); %do_graph(2016,qatar,Qatar); /* proc sql noprint; create table foo as select unique location from robsdata.WPP_data; quit; run; proc print data=foo; run; */ quit; ODS HTML CLOSE; ODS LISTING;