Hi All,
I have longitudinal data (long data format) and am trying to drop observations for people who have been lost to follow-up. I am defining this as people who missed 2 visits or more in a row. I made a "visitdiff" variable (which SHOULD be 1 if they have missed no visits), if it is >=2 then I'd like to drop all observations for that person moving forward since I am considering them lost to follow up.
Data Structure:
ID | visit | lag_visit | visitdiff |
1 | 1 | . | . |
1 | 2 | 1 | 1 |
1 | 5 | 2 | 3 |
1 | 6 | 5 | 1 |
1 | 7 | 6 | 1 |
1 | 8 | 7 | 1 |
1 | 9 | 8 | 1 |
2 | 1 | . | . |
2 | 2 | 1 | 1 |
2 | 3 | 2 | 1 |
3 | 1 | . | . |
3 | 2 | 1 | 1 |
3 | 3 | 2 | 1 |
3 | 5 | 3 | 2 |
3 | 6 | 5 | 1 |
In the data above, for id=1, I'd want to delete all data after visit 2 since the next visit was visit 5 (ie 2 missed visits). Here is the code I currently have to generate the visit differences based off of lag_visit. Any help on deleting all observations after the first instance of visitdiff>2 (since 2 missed visits would make visitdiff=3) for a participant would be greatly appreciated!
data Aim2.want; set Aim2.have;
by id visit;
lag_visit=lag(visit);
visitdiff=visit-lag_visit;
if first.id then visitdiff=.;
run;
UNTESTED CODE
data Aim2.want;
retain flag;
set Aim2.have;
by id visit;
lag_visit=lag(visit);
visitdiff=visit-lag_visit;
if first.id then do;
visitdiff=.;
flag=0;
end;
if visitdiff>=2 then flag=1;
run;
then you can delete all observations where flag=1
UNTESTED CODE
data Aim2.want;
retain flag;
set Aim2.have;
by id visit;
lag_visit=lag(visit);
visitdiff=visit-lag_visit;
if first.id then do;
visitdiff=.;
flag=0;
end;
if visitdiff>=2 then flag=1;
run;
then you can delete all observations where flag=1
Well, I said it was untested code. I don't think it should do that, can you show us the code you actually used and the result?
And can you provide your data as an actual SAS data step rather than in the form you provided it?
An example of how to provide data and possible solution:
Data have; input ID visit lag_visit visitdiff ; datalines; 1 1 . . 1 2 1 1 1 5 2 3 1 6 5 1 1 7 6 1 1 8 7 1 1 9 8 1 2 1 . . 2 2 1 1 2 3 2 1 3 1 . . 3 2 1 1 3 3 2 1 3 5 3 2 3 6 5 1 ; run; data want; set have; by id; retain drop ; if first.id then drop=0; if visitdiff>1 then drop=1; if drop=0; drop drop; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.