As featured in: Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study , and built from this github repository.
Chapter 8 Multiple Splits Parameterized.sas
1 /* "Chapter 8 Multiple Splits Parameterized.sas" from the SAS Press book
2  Data Management Solutions Using SAS Hash Table Operations:
3  A Business Intelligence Case Study
4 */
5 
6 %macro createHash
7  (hashTable = hashTable
8  ,parmfile = template.Chapter8ParmFile
9  );
10 
11  lr = 0;
12  dcl hash &hashTable(ORDERED:"A");
13  do while(lr=0);
14  set &parmfile end=lr;
15  where upcase(hashTable) = "%upcase(&hashTable)";
16  if Is_A_key then &hashTable..DefineKey(Column);
17  &hashTable..DefineData(Column);
18  end;
19  &hashTable..DefineDone();
20 
21 %mend createHash;
22 
23 proc sql noprint;
24  select distinct cats('%createHash(hashTable='
25  ,hashTable
26  ,")"
27  )
28  into:createHashCalls separated by " "
29  from template.Chapter8ParmFile;
30  select distinct cats("h_pointer="
31  ,hashTable
32  ,";"
33  ,"link slashline"
34  )
35  into:calcHash separated by ";"
36  from template.Chapter8ParmFile;
37  select distinct cats(hashTable
38  ,'.output(dataset:"_'
39  ,hashTable
40  ,'(drop=_:)")'
41  )
42  into:outputHash separated by ";"
43  from template.Chapter8ParmFile;
44  /*reset print;
45  select Name, Value from dictionary.macros
46  where name in ("CREATEHASHCALLS" "CALCHASH"
47  "OUTPUTHASH");*/
48 quit;
49 
50 data _null_;
51  /* define the lookup hash object tables */
52  dcl hash players(dataset:"dw.players(rename=(Player_ID=Batter_ID))"
53  ,multidata:"Y");
54  players.defineKey("Batter_ID");
55  players.defineData("Batter_ID","Team_SK","Last_Name","First_Name"
56  ,"Start_Date","End_Date");
57  players.defineDone();
58  dcl hash teams(dataset:"dw.teams");
59  teams.defineKey("Team_SK");
60  teams.defineData("Team_Name");
61  teams.defineDone();
62  dcl hash games(dataset:"dw.games");
63  games.defineKey("Game_SK");
64  games.defineData("Date","Month","DayOfWeek");
65  games.defineDone();
66  /* define the result hash object tables */
67  dcl hash h_pointer;
68 
69  &createHashCalls
70 
71  if 0 then set dw.players(rename=(Player_ID=Batter_ID))
72  dw.teams
73  dw.games;
74  format PAs AtBats Hits comma6. BA OBP SLG OPS 5.3;
75 
76  lr = 0;
77  do until(lr);
78  set dw.AtBats end = lr;
79  call missing(Team_SK,Last_Name,First_Name,Team_Name,Date,Month,DayOfWeek);
80  games.find();
81  players_rc = players.find();
82  do while(players_rc = 0);
83  if (Start_Date le Date le End_Date) then leave;
84  players_rc = players.find_next();
85  end;
86  if players_rc ne 0
87  then call missing(Team_SK,First_Name,Last_Name);
88  teams.find();
89  &calcHash;
90  end;
91  &outputHash;
92  stop;
93  slashline:
94  call missing(PAs,AtBats,Hits,_Bases,_Reached_Base);
95  rc = h_pointer.find();
96  PAs + 1;
97  AtBats + Is_An_AB;
98  Hits + Is_A_Hit;
99  _Bases + Bases;
100  _Reached_Base + Is_An_OnBase;
101  BA = divide(Hits,AtBats);
102  OBP = divide(_Reached_Base,PAs);
103  SLG = divide(_Bases,AtBats);
104  OPS = sum(OBP,SLG);
105  h_pointer.replace();
106  return;
107 run;