A follow-up SAS ODS Graphics step plot of electoral votes won by each candidate for each state using the latest results from NBC News.
* Fun With SAS ODS Graphics: 2024 Electoral College Results
Based on nbcnews.com/politics/2024-elections/president-results;
* Get Electoral College Votes and Most Recent Results by State
https://www.nbcnews.com/politics/2024-elections/president-results;
proc import datafile='/home/ted.conway/PresidentialElectionResults2024.xlsx' out=ElectionResults dbms=xlsx replace;
* Get state name to state code mapping;
proc import datafile='/home/ted.conway/state2statecd.xlsx' out=state2statecd dbms=xlsx;
proc sql; * Append state abbreviation (special join logic for split states, DC);
create table StateCodesElectionResults as
select s2c.statecd, case when harris>trump then 'Harris' else 'Trump' end as Winner, sum(er.votes) as Votes
from ElectionResults er
left join state2statecd s2c on translate(er.state,' ','123')=s2c.state or scan(er.state,-1,' ,')=s2c.statecd
group by winner, s2c.statecd order by winner;
* Get total electoral votes for each state for ranking (biggest first);
create table statevotes as select statecd, sum(votes) as numvotes from StateCodesElectionResults group by 1 order by 2 desc, 1;
create table rankstatevotes as select monotonic() as rank, sv.* from statevotes sv;
* Append rankings;
create table StateCodesElectionResultsRank as
select s.*, r.rank from StateCodesElectionResults s join rankstatevotes r on s.statecd=r.statecd
order by winner, rank;
data data2chart; * Get cumulative vote totals for chart for candidates;
set StateCodesElectionResultsRank;
by winner;
if first.winner then totvotes=0;
totvotes+votes;
proc sort data=data2chart; by rank winner; * Put data back in order for charting (states with most votesfirst);
%SGANNO; * Anotate chart with Wikipedia pics of Harris and Trump using SAS-provided macros;
data presidentpics; * Pics from https://en.wikipedia.org/wiki/2024_United_States_presidential_election;
%SGIMAGE (image="/home/ted.conway/Harris.png",drawspace="DATAVALUE",xc1="WY",y1=217,height=45,heightunit="DATA",anchor="TOPRIGHT");
%SGIMAGE (image="/home/ted.conway/Trump.png",drawspace="DATAVALUE",xc1="WY", y1=300, height=45,heightunit="DATA",anchor="TOPRIGHT");
%SGTEXT (LABEL="312",drawspace="DATAVALUE",xc1="SD", y1=253, textsize=16, textweight="bold",textcolor="red",anchor="TOP"); * Trump electoral votes;
%SGTEXT (LABEL="226",drawspace="DATAVALUE",xc1="SD", y1=170, textsize=16, textweight="bold",textcolor="blue",anchor="TOP"); * Harris electoral votes;
ods graphics / antialias height=11in width=14in; * Create step plot of electoral votes by state by candidate;
proc sgplot data=data2chart noborder nowall sganno=presidentpics;
styleattrs datacontrastcolors=(blue red); * Party colors;
inset "2024 ELECTORAL COLLEGE RESULTS" ' ' ' ' / textattrs=(size=24pt weight=bold) position=bottom;
step x=statecd y=totvotes / grouporder=data group=winner discreteoffset=0 datalabelpos=top justify=center datalabel=statecd markers filledoutlinedmarkers markerattrs=(symbol=circlefilled) lineattrs=(thickness=2pt);
xaxistable votes / label=" "; * Electoral votes for each state;
xaxis display=(nolabel noticks noline) grid valueattrs=(size=8.5pt) fitpolicy=none;
yaxis display=(noticks noline) label='ELECTORAL VOTES' grid valueattrs=(size=9pt) offsetmax=.075 values=(0 to 325 by 100);
keylegend / position=bottom location=inside noborder title=" "; * Legend for candidates;
refline 270 / axis=y label='270 to Win' labelloc=inside labelpos=min lineattrs=(thickness=2pt); * Votes needed to win;
run;
... View more