As featured in: Data Management Solutions Using SAS Hash Table Operations: A Business Intelligence Case Study , and built from this github repository.
Chapter 7 SCD 2.sas
1 /* "Chapter 7 SCD 2.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  if 0 then set template.Players_SCD2;
8  if _n_ = 1 then
9  do; /* define the hash table */
10  dcl hash scd(dataset:
11  ifc(exist("bizarro.Players_SCD2")
12  ,"bizarro.Players_SCD2"
13  ,"template.Players_SCD2"
14  )
15  ,ordered:"A",multidata:"Y");
16  scd.defineKey("Player_ID");
17  scd.defineData("Player_ID","Team_SK"
18  ,"First_Name","Last_Name"
19  ,"Position_Code","Bats","Throws"
20  ,"Start_Date","End_Date");
21  scd.defineDone();
22  end; /* define the hash table */
23 
24  set bizarro.atbats
25  (rename = (Batter_ID = Player_ID
26  Team_SK = _Team_SK
27  First_Name = _First_Name
28  Last_Name = _Last_Name
29  Position_Code = _Position_Code
30  Bats = _Bats
31  Throws = _Throws)
32  ) end=lr;
33 
34 
35  if scd.check() ne 0 then
36  do; /* need to add the player */
37  scd.add(key: Player_ID
38  ,data: Player_ID
39  ,data: _Team_SK
40  ,data: _First_Name
41  ,data: _Last_Name
42  ,data: _Position_Code
43  ,data: _Bats
44  ,data: _Throws
45  ,data: Date
46  ,data: &SCD_End_Date
47  );
48  end; /* need to add the player */
49  else
50  do; /* check to see if there are changes */
51 
52  RC = scd.find();
53  do while(RC = 0);
54  if (Start_Date le Date le End_Date) then leave;
55  RC = scd.find_next();
56  end;
57 
58  if catx(":", Team_SK, First_Name, Last_Name
59  , Position_Code, Bats, Throws) ne
60  catx(":",_Team_SK,_First_Name,_Last_Name
61  ,_Position_Code,_Bats,_Throws) then
62  do; /* date out prior record and add new one */;
63  if RC = 0 then scd.replaceDup(data: Player_ID
64  ,data: Team_SK
65  ,data: First_Name
66  ,data: Last_Name
67  ,data: Position_Code
68  ,data: Bats
69  ,data: Throws
70  ,data: Start_Date
71  ,data: Date-1
72  );
73  scd.add(key: Player_ID
74  ,data: Player_ID
75  ,data: _Team_SK
76  ,data: _First_Name
77  ,data: _Last_Name
78  ,data: _Position_Code
79  ,data: _Bats
80  ,data: _Throws
81  ,data: Date
82  ,data: &SCD_End_Date
83  );
84  end; /* date out prior record and add new one */;
85  end; /* check to see if there are changes */
86  if lr;
87  scd.output(dataset:"bizarro.Players_SCD2");
88  stop;
89 run;
90 
91 data tableLookup;
92  /* Sample Lookup */
93  if 0 then set bizarro.Players_SCD2;
94  if _n_ = 1 then
95  do;
96  dcl hash scd(dataset:"bizarro.Players_SCD2"
97  ,multidata:"Y");
98  scd.defineKey("Player_ID");
99  scd.defineData("Team_SK","Player_ID","First_Name"
100  ,"Last_Name","Position_Code","Bats"
101  ,"Throws","Start_Date","End_Date");
102  scd.defineDone();
103  end;
104  infile datalines;
105  attrib Date format = yymmdd10. informat = yymmdd10.;
106  input Player_ID Date;
107  RC = scd.find();
108  do while(RC = 0);
109  if (Start_Date le Date le End_Date) then leave;
110  RC = scd.find_next();
111  end;
112  if RC ne 0 then call missing(Team_SK,First_Name
113  ,Last_Name,Position_Code
114  ,Bats,Throws
115  ,Start_Date,End_Date);
116 datalines;
117 10103 2017/03/23
118 10103 2017/07/26
119 99999 2017/04/15
120 10782 2017/03/22
121 10782 2017/03/21
122 run;