Data visualization with SAS programming

Fun With SAS ODS Graphics: Super Bowl squares

Reply
Contributor tc
Contributor
Posts: 74

Fun With SAS ODS Graphics: Super Bowl squares

SQUARES.png

Sunday was Super Bowl LI, which got me to thinking: Could SAS ODS Graphics be used to generate Super Bowl squares?

 

*==> Generate Super Bowl Squares, Winnings (all score changes pay equal amount);

data Squares;                                    * Game players and # $2 squares desired;
input Player : $8. NumSquares@@;
do i=1 to numsquares;                            * Explode into 100 squares;
  rndnum=ranuni(14);                             * Asssign random # to each;
  output;
end;
cards;
BILL 10 SUSAN 5 MARTIN 5 JAMES 5 ANNE 10 SARAH 5 WILLIAM 5 RITA 5 
SAM 10 LILLY 5 TESSA 10 GEORGE 10 KATE 5 ANDREW 10
;
proc sort data=squares; by rndnum;

data AssignedSquares;                            * Assign scores to squares;
do patriots=0 to 9;
  do falcons=0 to 9;
    set squares;
    output;
  end;
end;

data events;                                     * Get Super Bowl scores (source: ESPN.com);
infile cards dlm=',' dsd;                        
input TimeOfScore : $10. Score : $10.@@;
if index(TimeOfScore,'Q') then Event='QTR  ';    * End of quarter?;
else if TimeOfScore='F' then Event='FINAL';      * Final score?;
else Event='CHG';                                * Otherwise, score change;
length ATL NE 3.;
ATL=mod(scan(score,1,'-'),10);                   * Falcons score (scores delimited by dash);
NE=mod(scan(score,2,'-'),10);                    * New England score;
* Source: http://www.espn.com/nfl/game?gameId=400927752;
* Time, Score (ATL-NE) - Events are: 1. Change in score, 2. End of quarter, 3. End of game;
cards;
1Q, 0-0, 12:15, 6-0, 12:15, 7-0, 8:48, 13-0, 8:48, 14-0, 2:21, 20-0, 2:36, 21-0, 0:02, 21-3
2Q, 21-3, 8:31, 27-3, 8:31, 28-3, 2:06, 28-9, 3Q, 28-9, 9:44, 28-12, 5:56, 28-18, 5:56, 28-20
0:57, 28-26, 0:57, 28-28, 4Q, 28-28, 11:02, 28-34, F, 28-34
;
                                                 * Find winning squares, # wins, winnings;
proc sql;                                        * Score changes pay $2*100/total #score changes;
create table AssignedSquaresEvents as
select a.patriots, a.falcons, a.player, count(e.event) as Wins,
       sum(case when e.event^='' then floor(100*2/numchgs) end) as Winnings  /* 100 $2 squares to split */
from assignedsquares a                                                       /* Round down to whole $ */
left join events e on a.patriots=e.NE and a.falcons=e.ATL and e.event='CHG'  /* Only score chages */
join (select count(*) as numchgs from events where event='CHG') t1 on 1=1    /* # of score changes */
group by 1, 2, 3, 4;

options missing="";
data AssignedSquaresEvents2;
retain x .5 y .5;                                * X-Y position of player names in charts;
retain x2 .975 y2 .025;                          * X-Y position of payout $ in charts;
set AssignedSquaresEvents;
Winningsx=put(winnings,dollar4.);                * Convert winnings to text string for charts;

ods listing image_dpi=200 gpath='/folders/myfolders'; 
ods graphics on / reset imagefmt=png noborder antialias width=12in height=12in imagename="SQUARES";

proc template;                                   * Let's generate some squares!;
define statgraph layoutdatalattice;
begingraph;
layout datalattice rowvar=patriots columnvar=falcons / 
  headerlabeldisplay=value rowheaders=left columnheaders=top columnheaders=top
  rowaxisopts=(display=none offsetmax=0 offsetmin=0 linearopts=(viewmin=0 viewmax=1)) 
  columnaxisopts=(display=none offsetmax=0 offsetmin=0 linearopts=(viewmin=0 viewmax=1)) 
  rowdatarange=unionall columndatarange=unionall headerlabelattrs=(weight=bold);
  sidebar / align=bottom; entry textattrs=(size=12pt weight=bold) "ATLANTA FALCONS" / valign=center; endsidebar;
  sidebar / align=right; entry textattrs=(size=12pt weight=bold) "NEW ENGLAND PATRIOTS" / valign=center rotate=90; endsidebar;
  layout prototype / aspectratio=1;
     textplot x=x y=y text=player / position=center textattrs=(size=14 color=blue weight=bold) rotate=45 contributeoffsets=none;
     textplot x=x2 y=y2 text=WinningsX / position=topleft textattrs=(size=14 color=green weight=bold) contributeoffsets=none;	
  endlayout;
endlayout;
endgraph;
end;
run;

proc sgrender data=AssignedSquaresEvents2 template=layoutdatalattice;
run;

* Documentation on layout data lattice can be found at 
  http://support.sas.com/documentation/cdl/en/grstatgraph/69718/HTML/default/viewer.htm#p1ug4om0rekm8vn1asfk7s8qb84g.htm; 

 

Ask a Question
Discussion stats
  • 0 replies
  • 1318 views
  • 1 like
  • 1 in conversation