%let name=us_traffic_fatalities; /* Set your current-working-directory (to read/write files), if you need to ... %let rc=%sysfunc(dlgcdir('c:\someplace\public_html')); */ filename odsout '.'; /* Read all Wikipedia tables from page. */ %inc '../democd101/readhtml4.sas'; %readhtml2( 'https://en.wikipedia.org/wiki/Motor_vehicle_fatality_rate_in_U.S._by_year' ); /* Table1 is "Motor Vehicle Deaths in US by year" */ data my_data (keep = year deaths bvmt population fatalities_100k); set table1 (where=(col5^=.)); year=1900+(_n_-1); format deaths comma20.0; deaths=.; deaths=input(col1,comma20.0); format bvmt comma20.2; bvmt=.; if index(col2,'.')^=0 then bvmt=input(col2,comma20.2); bvmt=input(col2,comma20.0); format population comma12.0; population=.; population=input(col4,comma12.0); format fatalities comma12.4; fatalities_100k=.; fatalities_100k=col5; run; /* The Wikipedia table had the wrong value for year 1900, therefore let's calculate our own... */ data my_data; set my_data; fatalities_100k=deaths/(population/100000); run; data anno_labels; input x_start y_start x_offset y_offset anchor $ 23-34 label $ 36-80; datalines; 1936.0 29.0 -7.0 -2.0 right safety glass 1942.0 15.8 -5.0 -3.8 topright WWII 1967.5 26.5 -5.5 0.5 bottomright seat belts 1998.0 16.0 1.3 2.5 bottom air bags 1973.5 20.5 -2.0 -3.0 topright 55mph limit 1980.0 23.2 2.0 2.5 bottomleft crash tests 2007.5 12.0 -2.0 -3.0 topright $4 gas ; run; data anno_labels; set anno_labels; length x1space y1space function $50 textcolor $12; x1space='datavalue'; y1space='datavalue'; x2space='datavalue'; y2space='datavalue'; function='line'; linecolor='blue'; linethickness=1; x1=x_start; y1=y_start; x2=x_start+x_offset; y2=y_start+y_offset; output; function='text'; textcolor='blue'; textsize=9; textweight='normal'; width=50; widthunit='percent'; x1=x_start+x_offset; y1=y_start+y_offset; output; run; ODS LISTING CLOSE; ODS HTML path=odsout body="&name..htm" (title="US Traffic Fatalities") style=htmlblue; ods graphics / imagemap tipmax=2500 imagefmt=png imagename="&name" width=1000px height=470px noborder; title1 color=gray33 height=15pt "US Motor Vehicle Deaths per 100,000 People"; footnote c=gray "Data source: Wikipedia"; proc sgplot data=my_data noborder sganno=anno_labels; series x=year y=fatalities_100k / markers markerattrs=(color=red size=6px symbol=circlefilled) lineattrs=(color=red); yaxis values=(0 to 30 by 5) valueattrs=(color=gray33 size=10pt) display=(noline noticks nolabel) offsetmax=0 offsetmin=0 grid; xaxis values=(1900 to 2020 by 10) valueattrs=(color=gray33 size=10pt) display=(noline noticks nolabel) offsetmax=0 offsetmin=0 grid; run; data my_data; set my_data; deaths_per_bvmt=deaths/bvmt; run; data anno_labels2; input x_start y_start x_offset y_offset anchor $ 25-36 label $ 38-80; datalines; 1936.0 130.0 -7.0 -20.0 topright safety glass 1942.0 85.0 -5.0 -20.0 topright WWII 1967.5 65.0 2.0 10.0 bottomleft seat belts 1973.5 48.5 2.0 10.0 bottomleft 55mph limit 1980.0 38.2 2.0 10.0 bottomleft crash tests 1998.0 25.0 2.0 10.0 bottomleft air bags 2007.5 22.0 2.0 10.0 bottomleft $4 gas ; run; data anno_labels2; set anno_labels2; length x1space y1space function $50 textcolor $12; x1space='datavalue'; y1space='datavalue'; x2space='datavalue'; y2space='datavalue'; function='line'; linecolor='blue'; linethickness=1; x1=x_start; y1=y_start; x2=x_start+x_offset; y2=y_start+y_offset; output; function='text'; textcolor='blue'; textsize=9; textweight='normal'; width=50; widthunit='percent'; x1=x_start+x_offset; y1=y_start+y_offset; output; run; title1 color=gray33 height=15pt "US Motor Vehicle Deaths per Billion Miles Traveled"; proc sgplot data=my_data noborder sganno=anno_labels2; series x=year y=deaths_per_bvmt / markers markerattrs=(color=red size=6px symbol=circlefilled) lineattrs=(color=red); yaxis values=(0 to 250 by 50) valueattrs=(color=gray33 size=10pt) display=(noline noticks nolabel) offsetmax=0 offsetmin=0 grid; xaxis values=(1920 to 2020 by 10) valueattrs=(color=gray33 size=10pt) display=(noline noticks nolabel) offsetmax=0 offsetmin=0 grid; run; quit; ODS HTML CLOSE; ODS LISTING;