How to Calculate Difference Between Dates in SAS

Deepanshu Bhalla Add Comment

The INTCK function in SAS can be used to calculate the difference between two dates in SAS.

The syntax of INTCK function is as follows:

INTCK(interval, start date, end data, method)

  • interval: Interval to calculate (day, week, month, quarter, year etc.)
  • start date: Starting SAS date
  • end date: Ending SAS date
  • method: This is optional argument. Intervals are counted using either a discrete or a continuous method. 'D' for discrete (Default), 'C' for continuous.)
Sample Dataset

Let's create a sample SAS dataset to demonstrate examples of this article. In this dataset, we have created two date variables - "startDate" and "endDate".

/*Sample Dataset*/
data mydata;
    format startDate endDate date9.;
    input startDate :date9. endDate :date9.;
    datalines;
01JUL2023 09SEP2023
10MAY2023 21JUN2023 
17JUL2023 04OCT2023
11FEB2023 04SEP2023
;
run;

/*Print Dataset*/
proc print data=mydata;
run;
Sample Dataset for INTCK function
Example 1: Calculate the Number of Days, Weeks and Months in SAS

You can specify the interval in the first argument of INTCK function. For example, specify "day" for calculating the number of days between dates in SAS. Also specify start and end date in the second and third argument of the function.

/*Calculate Difference between dates*/
data newdata;
    set mydata;
    days = intck('day', startDate, endDate);
    weeks = intck('week', startDate, endDate);
    months = intck('month', startDate, endDate);
run;

/*Print new dataset*/
proc print data=newdata;
run;
Calculate Difference Between Two Dates in SAS
Intervals

See the following table showing commonly used intervals in the INTCK function.

Interval Description
DAY Daily intervals
DAY3 Three-day intervals
WEEK Weekly intervals starting on Sundays
WEEK.7 Weekly intervals starting on Saturdays
WEEK.2 Weekly intervals starting on Mondays
WEEKDAY 5 days work week with a Saturday-Sunday weekend
WEEKDAY1W 6 days work week with Sunday as a weekend day
SEMIMONTH Half-month intervals
MONTH Monthly intervals
QTR Quarterly intervals
YEAR Yearly intervals
Example 2: Calculate the Number of WeekDays in SAS

To calculate the number of weekdays in SAS, you can specify "weekday" in the first argument of the INTCK function.

data newdata;
    set mydata;
    weekdays = intck('weekday', startDate, endDate);
run;

/*Print new dataset*/
proc print data=newdata;
run;
Calculate the Number of WeekDays in SAS
Methods to Calculate Difference between Dates in SAS

There are two methods in the INTCK function to calculate difference between dates in SAS. By default, SAS uses "discrete" method which is not a complete interval. The other method is "continuous" which you can read as complete interval.

data temp;
   joiningDay='30SEP2023'd;
   lastDay='01OCT2023'd;
   Months=intck('MONTH', joiningDay, lastDay, 'D');
   Months2=intck('MONTH', joiningDay, lastDay, 'C');   
   format joiningDay lastDay date9.;
   put Months= Months2=;
run;

Result: Months=1 Months2=0

When we use the default "discrete" method, SAS returns 1 month between 30th September and 1st October. Whereas the "continuous" method in the INTCK function returns 0 month.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to Calculate Difference Between Dates in SAS"
Next → ← Prev