DATA Step, Macro, Functions and more

Flagging Consecutive Data for Two Time Periods

Occasional Contributor thb
Occasional Contributor
Posts: 7

Flagging Consecutive Data for Two Time Periods

Hello SAS Community,


I have a methodology/coding question for my data.


The requirement is for a hospital to have at least 3 months of baseline data and 3 months of consecutive data to be included.  There is a specified baseline period, but if they don't have enough data, it can be outside of the baseline period IF there's 3 consecutive months.


For example,


Hospital ID10/1/1411/1/1412/1/149/1/1610/1/1611/1/1612/1/161/1/172/1/173/1/174/1/175/1/176/1/17


The baseline period is anything before September 2016.  The performance period is anything after October 2016.


Hospital 1 has a total of 6 months of data, but cannot be included because there are only 5 months of consecutive data.

Hospital 2 has 9 months of data, but some of the consecutive months fall into the performance period, but it's ok IF there are at least 3 months of consecutive performance data following this hospital's baseline period.  


The baseline and performance period for the hospitals would be:

Hospital 1Not includedNot included
Hospital 29/2016 - 11/201612/216 - 5/2017
Hospital 310/2016 - 12/20161/2017 - 3/2017
Hospital 410/2014 - 12/2014, 9/201610/2016 - 12/2016


What's the best way to code for this data?


Thank you for your help in advance.

Super User
Posts: 11,343

Re: Flagging Consecutive Data for Two Time Periods

Provide example data in the form of a data step. Since the "variables" you show are generally not valid SAS variable names you'll need to provide lots of information about your data set and the datastep is the best way.

Instructions here: will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Trusted Advisor
Posts: 1,586

Re: Flagging Consecutive Data for Two Time Periods

Assuming your variables are:   ID  M1 M2 M3 ... Mn

and each Mi variable is a sas date variable, then -

you may try next code:

%let date1 = 01OCT2014;
%let date2 = 01JUN2017;

data want;
   sey have;
         date1 = input("&date1",date9.);
         date2 = input("&date2",date9.);

         count_baseline = 0;
         count_performance = 0;
         include = 0;

          length baseline performance $20
          base_start  base_end perfm_start perfm_end 8;
         array  mx m1-m24;  /* assuming 24 month data */

        do i=1 to dim(mx);
             if count_baseline ge 3 and
                count_performance ge 3
                then include =1;
            if mx(i) = . and include = 0 then do;
               count_baseline = 0;
               count_performance = 0;    
            else do;  /* mx(i) = Y */
                datex = intnx('month', date1, i-1);
                if datex le '01sep2016'd then do; 
                   if count_baseline = 0  then base_start = datex;
                   base_end = datex;
                   count_baseline +1;
                else do;  /* datex ge '01OCT2016'd */
                   if count_performance  = 0 then perfm_start = datex;
                       perfm_end = datex;
                       count_performance  +1;     
          end;   /* ending loop over mx */                
         if include = 0 then do;
            baseline = 'Not Included';
            performance =   'Not Included';
         else do;
            baseline = catx(' - ',put(base_start,yymms7.),  
            performance = catx(' - ',put(perfm_start,yymms7.),
         keep ID baseline performance;
Trusted Advisor
Posts: 1,586

Re: Flagging Consecutive Data for Two Time Periods

I havn't used DATE2, though I have defined it.
You may limit your loop by it.
Ask a Question
Discussion stats
  • 3 replies
  • 3 in conversation