look both ways
DESCRIPTION
Look Both Ways. Dr. Arthur Tabachneck Director, Data Management. Note: program stolen from a SASOPEDIA article by Howard Schreier http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back. suppose you had the following data:. data have; input ID $ Measure; cards; A 11 A 12 A 13 - PowerPoint PPT PresentationTRANSCRIPT
TASS Meeting
Look Both Ways
June 12th, 2009
Look Both Ways
Dr. Arthur TabachneckDirector, Data Management
Note: program stolen from a SASOPEDIA article by Howard Schreierhttp://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back
TASS Meeting
Look Both Ways
June 12th, 2009
suppose you had the following data:
data have; input ID $ Measure; cards;A 11A 12A 13A 14B 21B 22B 23;
TASS Meeting
Look Both Ways
June 12th, 2009
and you needed to have the following table:
data need; input ID $ Measure Next_Measure Last_Measure; cards;A 11 12 .A 12 13 11A 13 14 12A 14 . 13B 21 22 .B 22 23 21B 23 . 22;
TASS Meeting
Look Both Ways
June 12th, 2009
that is, with the following assignments made:
ID Measure Next_Measure Last_Measure-- ------------ --------------------- --------------------A 11 12 .A 12 13 11A 13 14 12A 14 . 13B 21 22 .B 22 23 21B 23 . 22
TASS Meeting
Look Both Ways
June 12th, 2009
that is, with the following assignments made:
ID Measure Next_Measure Last_Measure-- ------------ --------------------- --------------------A 11 12 .A 12 13 11A 13 14 12A 14 13B 21 22 .B 22 23 21B 23 22
TASS Meeting
Look Both Ways
June 12th, 2009
that is, with the following assignments made:
ID Measure Next_Measure Last_Measure-- ------------ --------------------- --------------------A 11 12 A 12 13 11A 13 14 12A 14 . 13B 21 22 B 22 23 21B 23 . 22
TASS Meeting
Look Both Ways
June 12th, 2009
a data step solution
data need; set have; by ID; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure );run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #1:
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 11 1 0 . . 0 1
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #1:
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 11 1 0 12 . 0 1
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #1:
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 11 1 0 12 . 0 1
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #2:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 12 0 0 12 . 0 2
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #2:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 12 0 0 13 . 0 2
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #2:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 12 0 0 13 11 0 2
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #3:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 13 0 0 13 11 0 3
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #3:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 13 0 0 14 11 0 3
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #3:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 13 0 0 14 12 0 3
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #4:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 14 0 1 14 12 0 4
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #4:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 14 0 1 21 12 0 4
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #4:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_A 14 0 1 . 13 0 4
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #5:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 21 1 0 . 13 0 5
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #5:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 21 1 0 22 . 0 5
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #5:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 21 1 0 22 . 0 5
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #6:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 22 0 0 22 . 0 6
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #6:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 22 0 0 23 21 0 6
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #6:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 22 0 0 23 21 0 6
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #7:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 23 0 1 23 21 0 7
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #7:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 23 0 1 11 22 0 7
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
Why it works: Under the hood-Iteration #7:ID Measure FIRST.ID LAST.ID Next_Measure Last_Measure _ERROR_ _N_B 23 0 1 . 22 0 7
PDV
ID Measure-- ------------ A 11A 12A 13A 14B 21B 22 B 23
data need; set have; by ID; PutLog _all_ ; set have ( firstobs = 2 keep = Measure rename = (Measure = Next_Measure) ) have ( obs = 1 drop = _all_); PutLog _all_ ; Last_Measure = ifn( first.ID, (.), lag(Measure) ); Next_Measure = ifn( last.ID, (.), Next_Measure ); PutLog _all_ ;run;
TASS Meeting
Look Both Ways
June 12th, 2009
ending up with the following table
ID Measure Next_Measure Last_Measure-- ------------ --------------------- --------------------A 11 12 .A 12 13 11A 13 14 12A 14 . 13B 21 22 .B 22 23 21B 23 . 22
TASS Meeting
Look Both Ways
June 12th, 2009
Questions?
Your comments and questions are valued and encouraged.
Contact the author:
Dr. Arthur TabachneckDirector, Data ManagementInsurance Bureau of CanadaToronto, Ontario L3T 5K9Email: [email protected]