BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bgosiker
Obsidian | Level 7

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_visitvisitdiff
11..
1211
1523
1651
1761
1871
1981
21..
2211
2321
31..
3211
3321
3532
3651

 

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
bgosiker
Obsidian | Level 7
Thanks! When I try this is looks like it carries forward flag=1 through almost all of the dataset (multiple id's) instead of restarting at each id.
PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
bgosiker
Obsidian | Level 7
Sorry for delayed response -- I actually realized something other code that I was trying to institute in the same data step was messing it up a bit. Once I separated my other bits of code (deleting some specific observations) from it then the code you provided worked perfectly!
ballardw
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 5 replies
  • 993 views
  • 1 like
  • 3 in conversation