In SAS you can calculate the difference between two dates with the INTCK function. For example, you can calculate the number of days, months, years, etc. between two dates.
This article describes the INTCK function in detail and contains some useful examples.
The INTCK syntax
You can use the INTCK function to calculate the difference between two dates in SAS. This function requires you to define the interval, the start date, and the end date. Depending on the interval, it returns you the difference in days, weeks, months, etc.
INTCK(interval, start date, end date <, method>)
The INTCK functions consists of three obligatory arguments and one optional argument:
- interval: The name of the interval you use to calculate the difference between the SAS dates e.g. day, month, year. The interval must be in quotation marks. You can find a complete list of intervals here.
- start date: The first date you want to use to calculate the difference.
- end date: The second date you want to use to calculate the difference.
- method (optional): Specifies how the interval is interpreted. The possible methods are ‘D’ (discrete) or ‘C’ (continuous). The default method in SAS is ‘D’.
However, if you have a date-time variable instead of a date variable, your interval must contain ‘dt’. So, ‘dtday’, ‘dtmonth’, ‘dtyear’, etc.
Do you know? How to Convert a Datetime Variable into a Date Variable
How to Calculate the Difference Between Two SAS Dates
Below you find a couple of examples of how to use the INTCK function. Firstly, a simple data set will be created with two columns with dates. Secondly, the INTCK function will be used to calculate the difference in days between the two variables. Finally, two examples demonstrate the use of the method argument.
/* Create a data set with dates */ data work.ds; format mydate1 mydate2 date9.; input mydate1 :date9. mydate2 :date9.; datalines; 13JUN2020 18JUN2020 22JUN2020 20JUL2020 01JAN2020 31DEC2020 03MAY2020 19AUG2020 ; run;
data work.ds_day; set work.ds; /* Calculate the difference in days */ dif_days = intck('day', mydate1, mydate2); run;
data work.ds_month; set work.ds; /* Calculate the difference in months */ diff_months = intck('month', mydate1, mydate2); diff_months_disc = intck('month', mydate1, mydate2, 'D'); diff_months_cont = intck('month', mydate1, mydate2, 'C'); run;
If you set the method argument equal to ‘C’ when you calculate the difference in months, SAS calculates the number of complete months between two dates. So, although 22JUN2020 and 20JUL2020 belong to different months, the number of completed months between these dates is zero. In the example below, the same holds for the number of weeks past between 13JUN2020 (Saturday) and 18JUN2020 (Thursday). Although these dates belong to different weeks, the number of completed weeks between these two dates is zero.
data work.ds_week; set work.ds; /* Calculate the difference in weeks */ dif_weeks = intck('week', mydate1, mydate2); dif_weeks_disc = intck('week', mydate1, mydate2, 'D'); dif_weeks_cont = intck('week', mydate1, mydate2, 'C'); run;
If you want to calculate the previous or next day, week, month, etc. you can use the INTNX function. Check this article to learn more about this function.
Do you know? How to Easily Convert a Number into a SAS Date
You can find the official SAS documentation about the INTCK function here.
2 thoughts on “How to Easily Calculate the Difference Between Two SAS Dates”
Comments are closed.