Identifying overlapping date ranges in SAS

29 Views Asked by At

I am trying to detect clusters of events using SAS with the data coming from an Excel file. The criteria for a cluster is an event happening in the same facility, location within the facility, matching pathogens, and if patients were there at the same time. My variables are Facility_Name, location, admitDate, dischargeDate, pathogen1, pathogen2, and pathogen3. I think I have it narrowed down to the same facility, location, and pathogen, but I cannot seem to get the overlapping dates correct. Right now it is flagging if it overlaps with the previous date, but I want it to flag if it overlaps with the next date as well. I am using SAS 9.4 if that helps.

Here is my code:

proc sort data=clabsi;
  by Facility_Name Location Pathogen1 AdmitDate;
run;

proc sql;
  create table MatchingCombinations as
  select Facility_Name, Location, Pathogen1
  from clabsi
  group by Facility_Name, Location, Pathogen1
  having count(*) > 1;
quit;

data clabsi_filtered;
  merge clabsi(in=a) MatchingCombinations(in=b keep=Facility_Name Location Pathogen1);
  by Facility_Name Location Pathogen1;
  if a and b;
run;

proc print data=clabsi_filtered;
  var Facility_Name location admitDate dischargeDate pathogen1;
run;

data overlap (drop= lag_admitDate lag_dischargeDate);
  set clabsi_filtered;
  by Facility_Name;

  lag_admitDate = lag(admitDate);
  lag_dischargeDate = lag(dischargeDate);

  if not first.Facility_Name then do;
    if (lag_admitDate >= admitDate and lag_admitDate <= dischargeDate) or
       (lag_dischargeDate >= admitDate and lag_dischargeDate <= dischargeDate) or
       (lag_admitDate <= admitDate and lag_dischargeDate >= dischargeDate) then
      is_overlapping = 1;
    else
      is_overlapping = 0;
  end;

run;

proc print data=overlap;
var Facility_Name location admitDate dischargeDate pathogen1 is_overlapping;
run;

Here is a sample of the output:

35 FacilityA, and CCU-2W 03/07/2022 03/23/2022 SE . 
36 FacilityA, and CCU-2W 06/05/2022 06/16/2022 SE 0 
37 FacilityA, and CCU-2W 09/21/2022 10/08/2022 SE 0 
38 FacilityA, and CCU-P 01/07/2022 02/03/2022 ENTFS 0 
39 FacilityA, and CCU-P 12/02/2022 12/28/2022 ENTFS 0 
40 FacilityB BH51 03/03/2022 03/24/2022 SE . 
41 FacilityB BH51 07/08/2022 08/09/2022 SE 0 
42 FacilityC NICU 02/10/2022 04/18/2022 SE . 
43 FacilityC NICU 04/03/2022 07/27/2022 SE 1 
44 FacilityD F7N 01/12/2022 02/18/2022 EC . 
45 FacilityD F7N 05/04/2022 05/30/2022 EC 0 
46 FacilityD F7N 01/18/2022 02/19/2022 SA 0  
47 FacilityD F7N 01/28/2022 02/08/2022 SA 1 
0

There are 0 best solutions below