Search code examples
sassas-macro

SAS: Keeping observations that have a specified date interval


I have a dataset that looks something this:

    Unique_ID Date    
    1         03/23/1995
    1         03/27/1995
    1         04/14/1995
    1         08/29/1995
    1         02/14/1996
    .         .
    .         .
    .         .
    1         03/19/1997
    2         10/20/1993
    .         .
    .         .
    .         .
    2         04/20/2000

Within each Unique_ID I need to keep only those observations that have dates at least 3 months apart starting with the first observation (my data set is sorted by Unique_ID and Date). For example, within Unique_ID 1, I would need to keep the next observation that is at least 90 days from 3/23/1995, then the next observation that is 90 days from that day and so on. Could anybody point me in the right direction either with a macro or some kind of loop?


Solution

  • You can try something like this:

    data want;
       set have;
          by ID;
       retain date2find;
       if first.ID then do;
          output;   /* This statement will include the first obs from each    */
                    /* set of IDs if desired.  If not, delete this statement. */
          date2find = DATE + 90;
          end;
    
       if DATE >= date2find then do;
          output;                /* Output the found record    */
          date2find = DATE + 90; /* Re-set to date to be found */
          end;
       drop date2find;
    run;
    

    This relies on your dataset being sorted as described (by ID and DATE)