As featured in: Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study , and built from this github repository.
Chapter 9 HoH Multiple Splits.sas
1 /* "Chapter 9 HoH Multiple Splits.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 _null_;
7  dcl hash HoH(ordered:"A");
8  HoH.defineKey("hashTable");
9  HoH.defineData("hashTable","H","ITER","CalcAndOutput");
10  HoH.defineDone();
11  dcl hiter HoH_Iter("HoH");
12  dcl hash h();
13  dcl hiter iter;
14  /* define the lookup hash object tables */
15  do while(lr=0);
16  set template.chapter9lookuptables
17  template.chapter9splits(in = CalcAndOutput)
18  end=lr;
19  by hashTable;
20  if first.hashTable then
21  do; /* create the hash object instance */
22  if datasetTag ne ' ' then h = _new_ hash(dataset:datasetTag
23  ,multidata:"Y");
24  else h = _new_ hash(multidata:"Y");
25  end; /* create the hash object instance */
26  if Is_A_key then h.DefineKey(Column);
27  h.DefineData(Column);
28  if last.hashTable then
29  do; /* close the definition and add it to our HoH hash table */
30  h.defineDone();
31  HoH.add();
32  end; /* close the definition and add it to our HoH hash table */
33  end;
34  /* create non-scalar fields for the needed lookup tables */
35  HoH.find(key:"GAMES");
36  dcl hash games;
37  games = h;
38  HoH.find(key:"PLAYERS");
39  dcl hash players;
40  players = h;
41  HoH.find(key:"TEAMS");
42  dcl hash teams;
43  teams = h;
44 
45  if 0 then set dw.players
46  dw.teams
47  dw.games;
48  format PAs AtBats Hits comma6. BA OBP SLG OPS 5.3;
49 
50  lr = 0;
51  do until(lr);
52  set dw.AtBats(rename=(batter_id=player_id)) end = lr;
53  call missing(Team_SK,Last_Name,First_Name,Team_Name,Date,Month,DayOfWeek);
54  games.find();
55  players_rc = players.find();
56  do while(players_rc = 0);
57  if (Start_Date le Date le End_Date) then leave;
58  players_rc = players.find_next();
59  end;
60  if players_rc ne 0 then call missing(Team_SK,First_Name,Last_Name);
61  teams.find();
62  do while (HoH_Iter.next() = 0);
63  if not calcAndOutput then continue;
64  call missing(PAs,AtBats,Hits,_Bases,_Reached_Base);
65  rc = h.find();
66  PAs + 1;
67  AtBats + Is_An_AB;
68  Hits + Is_A_Hit;
69  _Bases + Bases;
70  _Reached_Base + Is_An_OnBase;
71  BA = divide(Hits,AtBats);
72  OBP = divide(_Reached_Base,PAs);
73  SLG = divide(_Bases,AtBats);
74  OPS = sum(OBP,SLG);
75  h.replace();
76  end;
77  end;
78  do while (HoH_Iter.next() = 0);
79  if not calcAndOutput then continue;
80  h.output(dataset:hashTable||"(drop=_:)");
81  end;
82  stop;
83 run;