Search code examples
timesasformatintervalsdifference

Find the interval in days between two dates SAS


I am trying to find the number of days between two dates, but I only get some dots in lieu of the days in the days column. The file is the following:

Start date  End date
7/5/2020    7/9/2020
7/12/2020   9/1/2020
7/27/2020   7/29/2020
7/11/2020   7/12/2020
7/15/2020   7/22/2020
7/16/2020   7/23/2020

Using this code:

DATA Dataset;set Dataset;
Format Start_date mmddyy8.;  
Format End_date mmddyy8.; /*The type of the two variables is Char and the format is MMDDYY after using the format code line*/
Days=INTCK('day', Start_date , End_date);
run;

Proc print data=Dataset;

I get:

Start_date   End_date   Days
7/5/20         7/9/20   .
7/12/20        9/1/20   .
7/27/20       7/29/20   .
7/11/20       7/12/20   .
7/15/20       7/22/20   .
7/16/20       7/23/20   .

Please, can someone help me if possible? Thank you in advance.


Solution

  • You cannot change a variable type with a format statement. You need to use input and convert the variables then do the operation.

    DATA Dataset1;*do not use the same data set name, it makes it harder to find errors;
    set Dataset;
    *convert to SAS dates;
    start_date_num = input(start_date, mmddyy10.);
    end_date_num = input(end_date, mmddyy10.);
    format start_date_num end_date_num date9.;
    
    *take the difference;
    Days=end_date_num - start_date_num;
    
    run;
    
    Proc print data=Dataset1;
    run;