Search code examples
datesasrange

Group cross date range in SAS


I need to mash the range of dates that cross over in SAS.

For example:

ID  PROV    dt_entrata  dt_uscita   FLAG
381 00001   31/12/2004  09/11/2005  OK
381 00001   05/07/2011  16/04/2012  OK
**381   00001   31/12/2013  20/10/2014  KO
381 00001   19/10/2014  15/02/2018  KO**

It must become:

ID  PROV    dt_entrata  dt_uscita   FLAG
381 00001   31/12/2004  09/11/2005  OK
381 00001   05/07/2011  16/04/2012  OK
**381   00001   31/12/2013  15/02/2018  OK**

Solution

  • This method expands the ranges then regroups and collapses back to ranges.

    data have;
       input ID $ PROV $ (dt_entrata dt_uscita)(:ddmmyy.)  FLAG $;
       cards;
    381 00001   31/12/2004  09/11/2005  OK
    381 00001   05/07/2011  16/04/2012  OK
    381 00001   31/12/2013  20/10/2014  KO
    381 00001   19/10/2014  15/02/2018  KO
    ;;;;
       run;
    data havev / view=havev;
       set have;
       do date = dt_entrata to dt_uscita;
          output;
          end;
       drop dt_: flag;
       run;
    proc sort data=havev out=exp nodupkey;
       by id prov date;
       run;
    
    data expV / view=expV;
       set exp;
       by id prov;
       run + dif(date) ne 1;
       if first.prov then run=1;
       format date ddmmyy10.;
       run;
    proc summary data=expv nway;
       by id prov;
       class run;
       output out=range(drop=_type_) min(date)=dt_entrata max(date)=dt_uscita;
       run;
    proc print;
       run;
    

    enter image description here