Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU)...

8
Saving Data Steps with th Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University

Transcript of Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU)...

Page 1: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

Saving Data Steps with theProc SQL Self-join

Yan Wang

Population Health Research Unit (PHRU)

Dalhousie University

Page 2: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

The Task

Obs ID indexdate exdate 1 1 28MAR2001 . 2 1 28APR2001 28APR2001 3 1 29JAN2004 . 4 2 28JUN1997 . 5 2 02JAN1998 02JAN1998 6 2 08JAN1998 08JAN1998 7 2 15FEB1998 15FEB1998 8 2 23FEB1998 . 9 2 11JUN1999 .

One data set with 2 dates: indexdate and exdateFor each individual, subsetting records whose indexdate in the 120 days before or 90 days after exdate

Page 3: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

proc sql; select a.*, (-90<= b.exdate-a.indexdate <=120) as diff from temp as a, temp as b where a.ID=b.ID;quit;

Obs ID indexdate exdate diff 1 1 28MAR2001 . 0 2 1 28APR2001 28APR2001 0 3 1 29JAN2004 . 0 4 1 28MAR2001 . 1 5 1 28APR2001 28APR2001 1 6 1 29JAN2004 . 0 7 1 28MAR2001 . 0 8 1 28APR2001 28APR2001 0 9 1 29JAN2004 . 0

Obs ID indexdate exdate 1 1 28MAR2001 . 2 1 28APR2001 28APR2001 3 1 29JAN2004 .

Obs ID indexdate exdate 1 1 28MAR2001 .

2 1 28APR2001 28APR2001 3 1 29JAN2004 .

Comparing

Page 4: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

Obs ID indexdate exdate diff Obs ID indexdate exdate diff 1 1 28MAR2001 . 0 21 2 11JUN1999 . 0 2 1 28APR2001 28APR2001 0 22 2 28JUN1997 . 0 3 1 29JAN2004 . 0 23 2 02JAN1998 02JAN1998 1 4 1 28MAR2001 . 1 24 2 08JAN1998 08JAN1998 1 5 1 28APR2001 28APR2001 1 25 2 15FEB1998 15FEB1998 1 6 1 29JAN2004 . 0 26 2 23FEB1998 . 1 7 1 28MAR2001 . 0 27 2 11JUN1999 . 0 8 1 28APR2001 28APR2001 0 28 2 28JUN1997 . 0 9 1 29JAN2004 . 0 29 2 02JAN1998 02JAN1998 1 10 2 28JUN1997 . 0 30 2 08JAN1998 08JAN1998 1 11 2 02JAN1998 02JAN1998 0 31 2 15FEB1998 15FEB1998 1 12 2 08JAN1998 08JAN1998 0 32 2 23FEB1998 . 1 13 2 15FEB1998 15FEB1998 0 33 2 11JUN1999 . 0 14 2 23FEB1998 . 0 34 2 28JUN1997 . 0 15 2 11JUN1999 . 0 35 2 02JAN1998 02JAN1998 0 16 2 28JUN1997 . 0 36 2 08JAN1998 08JAN1998 0 17 2 02JAN1998 02JAN1998 1 37 2 15FEB1998 15FEB1998 0 18 2 08JAN1998 08JAN1998 1 38 2 23FEB1998 . 0 19 2 15FEB1998 15FEB1998 1 39 2 11JUN1999 . 0 20 2 23FEB1998 . 1 40 2 28JUN1997 . 0 41 2 02JAN1998 02JAN1998 0 42 2 08JAN1998 08JAN1998 0 43 2 15FEB1998 15FEB1998 0 44 2 23FEB1998 . 0 45 2 11JUN1999 . 0

Page 5: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

proc sql; select distinct a.*, sum(-90<= b.exdate-a.indexdate <=120)>0 as index from temp as a, temp as b where a.ID=b.ID group by a.ID, a.indexdate;quit;

Obs ID indexdate exdate index 1 1 28MAR2001 . 1 2 1 28APR2001 28APR2001 1 3 1 29JAN2004 . 0 4 2 28JUN1997 . 0 5 2 02JAN1998 02JAN1998 1 6 2 08JAN1998 08JAN1998 1 7 2 15FEB1998 15FEB1998 1 8 2 23FEB1998 . 1 9 2 11JUN1999 . 0

Identifying

Page 6: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

proc sql; create table tp as select distinct a.*, sum(-90<= b.exdate-a.indexdate <=120)>0 as index from temp as a, temp as b where a.ID=b.ID group by a.ID, a.indexdate having index = 1;quit;

Subsetting

Obs ID indexdate exdate index 1 1 28MAR2001 . 1 2 1 28APR2001 28APR2001 1 3 2 02JAN1998 02JAN1998 1 4 2 08JAN1998 08JAN1998 1 5 2 15FEB1998 15FEB1998 1 6 2 23FEB1998 . 1

Page 7: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.

Reference

Subsetting SAS® Data Set by Using PROC SQL Self-join with Compound Key Zizhong Fan, Westat, Rockville, MD

http://www2.sas.com/proceedings/sugi29/065-29.pdf

Page 8: Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University.