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
![Page 1: Look Both Ways](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.site/reader034/viewer/2022051622/568151e5550346895dc01e44/html5/thumbnails/30.jpg)
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]