look both ways

30
TASS Meeting Look Both Ways June 12th, 2009 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

Upload: more

Post on 23-Jan-2016

30 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Look Both Ways

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

Page 2: Look Both Ways

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;

Page 3: Look Both Ways

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;

Page 4: Look Both Ways

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

Page 5: Look Both Ways

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

Page 6: Look Both Ways

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

Page 7: Look Both Ways

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;

Page 8: Look Both Ways

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

Page 9: Look Both Ways

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

Page 10: Look Both Ways

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

Page 11: Look Both Ways

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;

Page 12: Look Both Ways

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;

Page 13: Look Both Ways

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;

Page 14: Look Both Ways

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;

Page 15: Look Both Ways

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;

Page 16: Look Both Ways

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;

Page 17: Look Both Ways

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;

Page 18: Look Both Ways

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;

Page 19: Look Both Ways

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;

Page 20: Look Both Ways

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;

Page 21: Look Both Ways

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;

Page 22: Look Both Ways

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;

Page 23: Look Both Ways

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;

Page 24: Look Both Ways

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;

Page 25: Look Both Ways

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;

Page 26: Look Both Ways

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;

Page 27: Look Both Ways

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;

Page 28: Look Both Ways

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;

Page 29: Look Both Ways

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

Page 30: Look Both Ways

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]