Saving Data Steps with the Proc SQL Self-join Yan Wang Population Health Research Unit (PHRU) Dalhousie University
The Task ObsID indexdate exdate 11 28MAR APR APR JAN JUN JAN JAN JAN JAN FEB FEB FEB JUN1999. One data set with 2 dates: indexdate and exdate For each individual, subsetting records whose indexdate in the 120 days before or 90 days after exdate
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 MAR APR APR JAN MAR APR APR JAN MAR APR APR JAN Obs ID indexdate exdate MAR APR APR JAN2004. Obs ID indexdate exdate MAR APR APR JAN2004. Comparing
Obs ID indexdate exdate diff MAR JUN APR APR JUN JAN JAN JAN MAR JAN JAN APR APR FEB FEB JAN FEB MAR JUN APR APR JUN JAN JAN JAN JUN JAN JAN JAN JAN FEB FEB JAN JAN FEB FEB FEB JUN FEB JUN JUN JAN JAN JUN JAN JAN JAN JAN FEB FEB JAN JAN FEB FEB FEB JUN FEB JUN JAN JAN JAN JAN FEB FEB FEB JUN
proc sql; select distinct a.*, sum(-90 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 MAR APR APR JAN JUN JAN JAN JAN JAN FEB FEB FEB JUN Identifying
proc sql; create table tp as select distinct a.*, sum(-90 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 MAR APR APR JAN JAN JAN JAN FEB FEB FEB
Reference Subsetting SAS ® Data Set by Using PROC SQL Self-join with Compound Key Zizhong Fan, Westat, Rockville, MD