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

I have the following data:

 
Data sample;
	input id $ (diagnosis_date) (:mmddyy10.) date_diff1 date_diff2;
	format diagnosis_date date9.;
	datalines;
1 06/23/2007 . .
1 07/06/2007 13 13
1 09/26/2007 95 82
1 10/02/2007 101 6
2 04/07/2007 . .
2 04/28/2007 21 21
2 07/17/2007 100 79
2 08/05/2007 120 20
2 02/22/2008 220 120
;
run;
Screen Shot 2021-03-02 at 1.41.50 PM.png

Each row is an event (a disease diagnosis). I would like to keep the row with the index diagnosis and remove events that are within 90 days of the index and also within 90 days of each other. I have created two variables to try to help myself with this.date_diff1 is the number of days between the event and the index diagnosis; date_diff2 is the number of days between an event and the previous event before it. This is the dataset I am hoping to end up with.

Screen Shot 2021-03-02 at 1.42.24 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
PhilC
Rhodochrosite | Level 12

I would word my solution, which is what I think you mean, like this .  This removes "events that are within 90 days of the index and then within 90 days of each other, thereafter." 

 

A SAS loop like this is called a "Do-Witlock" loop.

 

data want;
  do until (last.ID);
    set sample ;
      by ID diagnosis_date;
    if first.ID 
      then _next_date=diagnosis_date;
    if _next_date<=diagnosis_date then do;
      output;
      _next_date=diagnosis_date+89;
    end;
  end;
  drop _:;
run;

View solution in original post

3 REPLIES 3
PhilC
Rhodochrosite | Level 12

I would word my solution, which is what I think you mean, like this .  This removes "events that are within 90 days of the index and then within 90 days of each other, thereafter." 

 

A SAS loop like this is called a "Do-Witlock" loop.

 

data want;
  do until (last.ID);
    set sample ;
      by ID diagnosis_date;
    if first.ID 
      then _next_date=diagnosis_date;
    if _next_date<=diagnosis_date then do;
      output;
      _next_date=diagnosis_date+89;
    end;
  end;
  drop _:;
run;
Doyinsola
Obsidian | Level 7

This seems to work irrespective of the number of events an ID has. Perfect, thank you!

ChrisNZ
Tourmaline | Level 20

Like this?

data WANT; 
  set SAMPLE;
  by ID;
  retain REF_DATE;
  if first.ID | (ID=lag(ID) and DIAGNOSIS_DATE-REF_DATE > 90) then do;
    output;
    REF_DATE=DIAGNOSIS_DATE;
  end;
run;
ID DIAGNOSIS_DATE DATE_DIFF1 DATE_DIFF2
1 23JUN2007 . .
1 26SEP2007 95 82
2 07APR2007 . .
2 17JUL2007 100 79
2 22FEB2008 220 120
 
 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 667 views
  • 1 like
  • 3 in conversation