As featured in: Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study , and built from this github repository.
Chapter 8 Count Consecutive Events.sas
1 /* "Chapter 8 Count Consecutive Events.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 Consecutive_Hits;
7  keep Consecutive_Hits Exact_Count Total_Count;
8  format Consecutive_Hits 8.
9  Exact_Count Total_Count comma10.;
10  retain Exact_Count 1;
11  if _n_ = 1 then
12  do; /* define the hash table */
13  dcl hash consecHits(ordered:"D",suminc:"Exact_Count");
14  consecHits.defineKey("Consecutive_Hits");
15  consecHits.defineDone();
16  end; /* define the hash table */
17  Consecutive_Hits = 0;
18  do until(last.Top_Bot);
19  set dw.atbats(keep=Game_SK Inning Top_Bot Is_A_Hit) end=lr;
20  by Game_SK Inning Top_Bot notsorted;
21  Consecutive_Hits = ifn(Is_A_Hit,Consecutive_Hits+1,0);
22  if Is_A_Hit then consecHits.ref();
23  end;
24  if lr;
25  Total_Adjust = 0;
26  do Consecutive_Hits = consecHits.num_items to 1 by -1;
27  consecHits.sum(sum:Exact_Count);
28  Total_Count = Exact_Count + Total_Adjust;
29  output;
30  Total_Adjust + Exact_Count;
31  end;
32 run;