Search code examples
datesasmaxminimum

How to find min and max date across columns in SAS data set?


I have a data set of following type

data have;
input subjid  var1$ var2$ var3$ var4$ var5$ var6$;
datalines;
121 23jan2022 24jan2022 20jan2022  24jan2022 26jan2022 25jan2022 
122 20jan2022 22jan2022 26jan2022  28jan2022 23jan2022 27jan2022 
;
run;

enter image description here

How can find the minimum date in all row across var1 to var3 and maximum date across var4 to var6 and then find the difference between maximum and minimum date.

The expected outcome is enter image description here

In first row, 6 is the difference of 26jan2022 (this is max of var4-var6) and 20jan2022 (this is min date of var1-var3)

Any help is appreciated.


Solution

  • If the variables are actual date values.

    data have;
      input subjid $  (var1-var6) (:date.);
      format var1-var6 date9.;
    datalines;
    121 23jan2022 24jan2022 20jan2022  24jan2022 26jan2022 25jan2022 
    122 20jan2022 22jan2022 26jan2022  28jan2022 23jan2022 27jan2022 
    ;
    

    Then just use the MIN() and MAX() functions to find the min and max dates. Once you have those simple subtraction will find the difference (range of dates).

    data want;
      set have;
      mindate = min(of var1-var3);
      maxdate = max(of var4-var6);
      diff = maxdate-mindate;
      format mindate maxdate date9.;
    run;
    

    Results

    enter image description here