As featured in: Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study , and built from this github repository.
Chapter 12 Pitcher Metrics What-If.sas
1 /* "Chapter 12 Pitcher Metrics What-If.sas" from the SAS Press book
2  Data Management Solutions Using SAS Hash Table Operations:
3  A Business Intelligence Case Study
4 */
5 
6 data chapter9splits;
7  set template.chapter9splits;
8  by hashTable;
9  output;
10  if last.hashTable;
11  Column = "IP";
12  output;
13  Column = "ERA";
14  output;
15  Column = "WHIP";
16  output;
17  Column = "_Runs";
18  output;
19  Column = "_Outs";
20  output;
21  Column = "_Walks";
22  output;
23  Column = "_HBP";
24  output;
25  Column = "WHIP_HBP";
26  output;
27  Column = "BASES_IP";
28  output;
29 run;
30 
31 data _null_;
32  dcl hash HoH(ordered:"A");
33  HoH.defineKey("hashTable");
34  HoH.defineData("hashTable","H","calcAndOutput");
35  HoH.defineDone();
36  dcl hiter HoH_Iter("HoH");
37  dcl hash h();
38  dcl hiter iter;
39  /* define the lookup hash object tables */
40  do while(lr=0);
41  set template.chapter9lookuptables
42  chapter9splits(in=CalcAndOutput)
43  end=lr;
44  by hashTable;
45  if first.hashTable then
46  do; /* create the hash object instance */
47  if datasetTag ne ' ' then h = _new_ hash(dataset:datasetTag
48  ,multidata:"Y");
49  else h = _new_ hash(multidata:"Y");
50  end; /* create the hash object instance */
51  if Is_A_key then h.DefineKey(Column);
52  h.DefineData(Column);
53  if last.hashTable then
54  do; /* close the definition and add it to our HoH hash table */
55  h.defineDone();
56  HoH.add();
57  end; /* close the definition and add it to our HoH hash table */
58  end;
59  /* create non-scalar fields for the lookup tables */
60  HoH.find(key:"GAMES");
61  dcl hash games;
62  games = h;
63  HoH.find(key:"PLAYERS");
64  dcl hash players;
65  players = h;
66  HoH.find(key:"TEAMS");
67  dcl hash teams;
68  teams = h;
69  dcl hash pitchers(dataset:"dw.pitches(rename=(pitcher_id = Player_ID))");
70  pitchers.defineKey("game_sk","top_bot","ab_number");
71  pitchers.defineData("player_id");
72  pitchers.defineDone();
73 
74  if 0 then set dw.players
75  dw.teams
76  dw.games
77  dw.pitches;
78  format PAs AtBats Hits comma6. BA OBP SLG OPS 5.3
79  IP comma6. ERA WHIP WHIP_HBP BASES_IP 6.3;
80 
81  lr = 0;
82  do until(lr);
83  set dw.AtBats end = lr;
84  call missing(Team_SK,Last_Name,First_Name,Team_Name,Date,Month,DayOfWeek);
85  games.find();
86  pitchers.find();
87  players_rc = players.find();
88  do while(players_rc = 0);
89  if (Start_Date le Date le End_Date) then leave;
90  players_rc = players.find_next();
91  end;
92  if players_rc ne 0 then call missing(Team_SK,First_Name,Last_Name);
93  teams.find();
94 
95  do while (HoH_Iter.next() = 0);
96  if not calcAndOutput then continue;
97  call missing(PAs,AtBats,Hits,_Bases,_Reached_Base
98  ,_Outs,_Runs,_Bases,_HBP);
99  rc = h.find();
100  PAs + 1;
101  AtBats + Is_An_AB;
102  Hits + Is_A_Hit;
103  _Bases + Bases;
104  _Reached_Base + Is_An_OnBase;
105  _Outs + Is_An_Out;
106  _Runs + Runs;
107  _Walks + (Result = "Walk");
108  _HBP + (Result = "Hit By Pitch");
109  BA = divide(Hits,AtBats);
110  OBP = divide(_Reached_Base,PAs);
111  SLG = divide(_Bases,AtBats);
112  OPS = sum(OBP,SLG);
113  if _Outs then
114  do; /* calculate pitcher metrics suppressing missing value note */
115  IP = _Outs/3;
116  ERA = divide(_Runs*9,IP);
117  WHIP = divide(sum(_Walks,Hits),IP);
118  WHIP_HBP = divide(sum(_Walks,Hits,_HBP),IP);
119  BASES_IP = divide(_Bases,IP);
120  end; /* calculate pitcher metrics missing value note */
121  h.replace();
122  end;
123  end;
124  do while (HoH_Iter.next() = 0);
125  if not calcAndOutput then continue;
126  h.output(dataset:hashTable||"(drop=_:)");
127  end;
128  stop;
129 run;