Monday, March 16, 2009

LOCF by using only PROC SORT

LOCF is short for Last Observation Carried Forward. It is a common method in longitudinal studies. And there are many ways to handle this. Here is one tip of using NODUPKEY option.

/* Classical way */
proc sort data=inds;
by subjid DESCENDING visitnum;
run;
data locf;
set inds (where=(visitnum <=3));
by subjid visitnum;

retain base;
if first.subjid then base=.;
if not missing(value) then base=value;
if last.subjid;
run;

/* Use NODUPKEY option in PROC SORT. */
proc sort data=inds;
by subjid DESCENDING visitnum;
run;
proc sort data=inds(where=(visitnum <=3 and not missing(value))) out=locf NODUPKEY;
by subjid;
run;

No comments: