%let name=us_election_by_county; filename odsout '.'; /* SAS version of: https://upload.wikimedia.org/wikipedia/commons/d/da/2016_Presidential_Election_by_County.svg */ /* Prepare the map of the 48 contiguous US states */ data us_map; set mapsgfk.us_counties (where=(statecode not in ('AK' 'HI')) drop=resolution); if x=. then density=0; /* make sure you don't get rid of 'hole' indicators, in Virginia! */ /* this would run quicker if I subset by density now, but I can't do it until later, or the gremove isn't going to work correctly. */ /* if density<=2 then output; */ run; proc gproject data=us_map out=us_map latlong eastlong degrees dupok; id state county; run; /* Project, re-size & move Alaska */ data ak_map; set mapsgfk.us_counties (where=(density<=3 and statecode='AK') drop=resolution); run; proc gproject data=ak_map out=ak_map latlong eastlong degrees dupok nodateline longmin=-169; id state county; run; data ak_map; set ak_map; x=x*.40; y=y*.40; x=x-.28; y=y-.15; run; /* Project, re-size & move Hawaii */ data hi_map; set mapsgfk.us_counties (where=(statecode='HI') drop=resolution); run; proc gproject data=hi_map out=hi_map latlong eastlong degrees dupok nodateline; id state county; run; data hi_map; set hi_map; x=x*.80; y=y*.80; x=x-.28; y=y-.12; run; /* Now, combine the continental US/AK/HI */ data my_map; set us_map ak_map hi_map; original_order=_n_; run; /* Using 12-16 (December 2016) data from github ... https://github.com/tonmcg/County_Level_Election_Results_12-16 https://github.com/tonmcg/County_Level_Election_Results_12-16/blob/master/2016_US_County_Level_Presidential_Results.csv (specifically, the 12-16 'raw data' link, as as used in the filename statement below) */ /* Read the data from the Internet */ filename myfile url "https://raw.githubusercontent.com/tonmcg/County_Level_Election_Results_12-16/master/US_County_Level_Presidential_Results_12-16.csv?_sm_au_=iVVwnrqj5PSHv3vH"; data my_data (drop = whole_line combined_fips); infile myfile dlm=' ' firstobs=2 truncover; input whole_line $ 1-500; combined_fips=.; combined_fips=scan(whole_line,2,','); state=.; state=substr(put(combined_fips,z5.),1,2); county=.; county=substr(put(combined_fips,z5.),3,3); statecode=fipstate(state); votes_dem_2016=.; votes_dem_2016=scan(whole_line,3,','); votes_gop_2016=.; votes_gop_2016=scan(whole_line,4,','); length winner $7; if votes_dem_2016>votes_gop_2016 then winner='Hillary'; else winner='Trump'; run; /* merge in the county name for each numeric county id# ... */ proc sql noprint; create table my_data as select my_data.*, us_counties_attr.idname as name from my_data left join mapsgfk.us_counties_attr on my_data.state=us_counties_attr.state and my_data.county=us_counties_attr.county; quit; run; /* create hover-text */ data my_data; set my_data; length my_html $500; my_html= 'title='||quote( trim(left(propcase(name)))||' County, '||trim(left(statecode))||'0d'x|| 'Trump: '||trim(left(put(votes_gop_2016,comma8.0)))||'0d'x|| 'Hillary: '||trim(left(put(votes_dem_2016,comma8.0)))); run; /* annotate state outlines on the county map */ proc gremove data=my_map out=anno_outline; id state county; by state notsorted; run; data anno_outline; set anno_outline (where=(density<=2)); by state segment notsorted; xsys='2'; ysys='2'; hsys='3'; when='a'; length function $8; color="white"; style='empty'; if first.state or first.segment then function='poly'; else function='polycont'; run; data my_map; set my_map (where=(density<=2)); run; goptions device=png; goptions border; goptions xpixels=1100 ypixels=700; ODS LISTING CLOSE; ODS HTML path=odsout body="&name..htm" (title="US 2016 Election Map, by county") style=htmlblue; goptions gunit=pct htitle=4.5 ftitle="Albany amt/bold" htext=2.0 ftext="albany amt"; goptions ctext=gray33; legend1 position=(top right) mode=share across=1 label=none shape=bar(.15in,.15in) offset=(-17,-7) order=descending; pattern1 v=s c=cx0057e2; pattern2 v=s c=cxde0100; title1 ls=1.5 "2016 US Presidential Election Winner, by County"; title2 a=-90 h=5 ' '; proc gmap data=my_data map=my_map anno=anno_outline; note move=(28,1) c=gray link='https://github.com/tonmcg/County_Level_Election_Results_12-16' 'Data source: https://github.com/tonmcg/County_Level_Election_Results_12-16'; id state county; choro winner / legend=legend1 coutline=cxc25979 html=my_html des='' name="&name"; run; quit; ODS HTML CLOSE; ODS LISTING;