SAS Functions SAS How To's

Complete Guide for SAS INTCK Function – Many Examples

If you work in SAS with Date or DateTime variables, sooner or later, you will need the INTCK function. With the INTCK function, you can calculate the difference between two dates in days, months, year, etc. In this article, we discuss the syntax of the SAS INTCK function and provide many examples of real-world problems.

If you want to know how to add days, weeks, months, etc. to an existing date variable, then you need the INTNX function.

The SAS INTCK Function: Syntax

INTCK(interval, start-date, end-date, <method>)

The INTCK function has three obligatory arguments and one options argument:

  • interval: a character constant, variable, or expression (in lower or uppercase) that specifies your interval, e.g. “day” or “month”. This page lists all possible intervals.
  • start-date: a Date or DateTime expression.
  • end-date: a Date or DateTime expression.
  • method (optional): specifies that intervals are counted using either a discrete or a continuous method. The default method is Discrete.

The SAS INTCK Function: Examples

In this section, we give examples of the most common uses of the SAS INTCK function. In all examples, the start-date and the end-date arguments are Date variable. However, if the arguments aren’t of the same type, you need to convert one of them. In other articles on this website, we explain how to convert a Date variable into a DateTime variable and vice versa.

Difference Between Two Dates in DAYS

SAS INTCK: Difference in Days example

You can use the SAS INTCK function to calculate the difference between two dates in days setting the interval argument equal to “day”. If you work with DateTime variables, then you use “dtday” instead of “day”. In this example, we determine the number of days between the 1st of July 2020 and the 3rd of July 2020.

/* DIFFERENCE IN DAYS */
DATA _NULL_;
	DIFF_DAYS = INTCK("DAY", "1JUL2020"D, "3JUL2020"D);
	PUT "DIFFERENCE IN DAYS: " DIFF_DAYS;
RUN;

DIFFERENCE IN DAYS: 2

Difference Between Two Dates in WEEKS

SAS INTCK: Difference in Weeks example

You can use the argument “WEEK” in the INTCK function to obtain the number of weeks between two dates. The code below shows how you can calculate the number of weeks between the 1st of July and the 13th of July. As you can see in the image above the difference is two weeks (week 27 vs. week 29).

/* DIFFERENCE IN WEEKS */
DATA _NULL_;
	DIFF_WEEKS = INTCK("WEEK", "1JUL2020"D, "13JUL2020"D);
	PUT "DIFFERENCE IN WEEKS: " DIFF_WEEKS;
RUN;

DIFFERENCE IN WEEKS: 2

However, if you want SAS to return the number of whole weeks between two dates, then you need to change the optional method argument. By default, the method that SAS uses to count the difference between days, weeks, months, etc. is discrete. However, if you want SAS to return the number of whole weeks between the 1st of July and the 13th of July, you need to set the method argument to “continuous”.

/* DIFFERENCE IN WEEKS (CONTINUOUS) */
DATA _NULL_;
	DIFF_WEEKS_CONT = INTCK("WEEK", "1JUL2020"D, "13JUL2020"D, "CONTINUOUS");
	PUT "DIFFERENCE IN WHOLE WEEKS: " DIFF_WEEKS_CONT;
RUN;

DIFFERENCE IN WHOLE WEEKS: 1

For more information about the method argument, go to a later section of the article.

Difference Between Two Dates in MONTHS

SAS INTCK: Difference in Months example

As in the previous examples, you need to change the interval argument. To count the number of months between two dates you need to set the argument to “month”. If you use DateTime variables, you use “dtmonth”.

/* DIFFERENCE IN MONTHS */
DATA _NULL_;
	DIFF_MONTHS = INTCK("MONTH", "1JUL2020"D, "24AUG2020"D);
	PUT "DIFFERENCE IN MONTHS: " DIFF_MONTHS;
RUN;

DIFFERENCE IN MONTHS: 1

Difference Between Two Dates in QUARTERS

SAS INTCK: Difference in Quarters example

You can also use the INTCK function to count the number of quarters between two dates. We can calculate the difference between the 1st of July 2020 (Q3) and the 1st of December 2020 (Q4) with the code below. You need to set the interval parameter to “qtr” or “dtqtr” if you work with DateTime variables.

/* DIFFERENCE IN QUARTERS */
DATA _NULL_;
	DIFF_QUARTERS = INTCK("QTR", "1JUL2020"D, "1DEC2020"D);
	PUT "DIFFERENCE IN QUARTERS: " DIFF_QUARTERS;
RUN;

DIFFERENCE IN QUARTERS: 1

Difference Between Two Dates in YEAR

SAS INTCK: Difference in Years example

You can use the SAS INTCK function also to calculate the number of years between two dates. If you work with Date variables you set the interval to “year”. In case your variables are Datetime, then you need to set this parameter to “dtyear”. The example below shows the code on how to calculate the difference in years between the 1st of July 2019 and the 1st of July 2020.

/* DIFFERENCE IN YEARS */
DATA _NULL_;
	DIFF_YEARS = INTCK("YEAR", "1JUL2019"D, "1JUL2020"D);
	PUT "DIFFERENCE IN YEARS: " DIFF_YEARS;
RUN;

DIFFERENCE IN YEARS: 1

INTCK: Special Intervals

Besides the well-known interval arguments mentioned above (day, week, month, etc.), You can use the INTCK function also for less-known purposes. For example, to calculate the number of business days or the number of Tuesdays between two dates. Below we show some examples of how to use the SAS INTCK function in cases.

Difference Between Two Dates in BUSINESS DAYS

INTCK: Difference in business days

For example, the number of business days (weekdays) between the 1st of July 2020 (Wednesday) and the 7th of July 2020 (Thursday) is four. SAS assumes a workweek starts on Monday and ends on Friday. The code snippet below demonstrates how to count the difference in business days setting the interval argument to “weekday”.

/* DIFFERENCE IN BUSINESS DAYS */
DATA _NULL_;
	DIFF_BUSINESSDAYS = INTCK("WEEKDAY", "1JUL2020"D, "7JUL2020"D);
	PUT "DIFFERENCE IN BUSINESS DAYS: " DIFF_BUSINESSDAYS;
RUN;

DIFFERENCE IN BUSINESS DAYS: 4

As mentioned before, SAS assumes that a working week has 5 days and that Saturday and Sunday are weekends. However, if you need a working week of 6 days where only Sunday is weekend, you can set the interval argument to “weekday1w”.

In the SAS programming language, the first day of the week is Sunday, Monday is the second day, and so on. With the “weekday1w” you specify that day 1 of the week is a weekend. Using this syntax, you can create your definition of a business week. For example, “weekday123w” means that Sunday (day 1), Monday (day 2) and Tuesday (day 3) are weekends and that the rest of the week are business days.

/* DIFFERENCE IN CUSTOM BUSINESS DAYS */
DATA _NULL_;
	DIFF_BUSINESSDAYS = INTCK("WEEKDAY123W", "1JUL2020"D, "7JUL2020"D);
	PUT "DIFFERENCE IN CUSTOM BUSINESS DAYS: " DIFF_BUSINESSDAYS;
RUN;

DIFFERENCE IN CUSTOM BUSINESS DAYS: 3

Difference Between Two Dates in MONDAYS / TUESDAYS / ETC.

SAS INTCK: Difference in Weeks example

In a previous example, we showed how to determine the number of weeks between the 1st of July 2020 and the 13th of July 2020. SAS returned 2 as the difference. However, suppose you don’t want to count the number of weeks. Instead, you want to know how many Tuesdays there are between these two dates. You can do this by setting the interval parameter to “week.3”. Remember that in the SAS language, Tuesday is the third day of the week.

/* DIFFERENCE IN TUESDAYS */
DATA _NULL_;
	DIFF_TUESDAYS = INTCK("WEEK.3", "1JUL2020"D, "13JUL2020"D);
	PUT "DIFFERENCE IN TUESDAYS: " DIFF_TUESDAYS;
RUN;

DIFFERENCE IN TUESDAYS: 1

As you can verify with the calendar above, SAS calculated the number of Tuesdays between the 1st of July 2020 and the 13th of July 2020 correctly.

Calculating the number of, for example, Tuesdays between two dates is equivalent to calculating the difference in weeks between two dates where the week starts on Tuesday. We will show this in the example below.

INTCK: Week starts on Monday

As mentioned before, weeks start on Sunday in SAS. However, in a lot of regions and/or for business purposes it’s normal to assume that weeks start on Monday. If you calculate the difference in weeks between two dates, you can let SAS know on which day the week starts. In the example below, we show the difference in results if we assume that the week starts either on Sunday or on Monday.

/* DIFFERENCE IN WEEKS (WEEK STARTS ON SUNDAY)*/
DATA _NULL_;
	DIFF_WEEKS = INTCK("WEEK", "5JUL2020"D, "7JUL2020"D);
	PUT "DIFFERENCE IN WEEKS (WEEK STARTS ON SUNDAY): " DIFF_WEEKS;
RUN;
 
/* DIFFERENCE IN WEEKS (WEEK STARTS ON MONDAY)*/
DATA _NULL_;
	DIFF_WEEKS = INTCK("WEEK.2", "5JUL2020"D, "7JUL2020"D);
	PUT "DIFFERENCE IN WEEKS (WEEK STARTS ON MONDAY): " DIFF_WEEKS;
RUN;

DIFFERENCE IN WEEKS (WEEK STARTS ON SUNDAY): 0
DIFFERENCE IN WEEKS (WEEK STARTS ON MONDAY): 1

INTCK: The Method Argument

As mentioned in the syntax section of this article, the INTCK function has one optional argument, namely, the method argument. By default, this argument is set to “discrete” or “d” which means that SAS counts the number of days, week, months, etc. between two dates. However, if you set this argument to “continuous” or “c”, then SAS counts the number of complete days, weeks, months, etc. between two days. We show the difference with the example code below.

SAS INTCK: Method argument "discrete" or "continuous"
/* THE METHOD ARGUMENT */
DATA WORK.METHOD_ARGUMENT;
	START = '1JUL2020'D;
	END = '5JUL2020'D;
	DIFF_WEEKS_DEFAULT = INTCK("WEEK", START, END);
	DIFF_WEEKS_DISCRETE = INTCK("WEEK", START, END, "D");
	DIFF_WEEKS_CONTINUOUS = INTCK("WEEK", START, END, "C");
	OUTPUT;
 
	END = '9JUL2020'D;
	DIFF_WEEKS_DEFAULT = INTCK("WEEK", START, END);
	DIFF_WEEKS_DISCRETE = INTCK("WEEK", START, END, "D");
	DIFF_WEEKS_CONTINUOUS = INTCK("WEEK", START, END, "C");
	OUTPUT;
 
	END = '14JUL2020'D;
	DIFF_WEEKS_DEFAULT = INTCK("WEEK", START, END);
	DIFF_WEEKS_DISCRETE = INTCK("WEEK", START, END, "D");
	DIFF_WEEKS_CONTINUOUS = INTCK("WEEK", START, END, "C");
	OUTPUT;
 
	FORMAT START END DATE9.;
RUN;
SAS INTCK result method argument

INTCK & Macro Functions

If you write more complex code, you probably create macro variables and/or macro functions. To use the INTCK function, you need the %synfunc-function. Also, you need to write the interval and method arguments without quotes. See the examples below on how to use the SAS INTCK function in a macro.

/* INTCK FUNCTION IN MACROS */
%MACRO DIFFERENCE_MACRO(START_DATE, END_DATE);
DATA WORK.DS_DIFF_MACRO;
	START="&amp;START_DATE."D;
	END="&amp;END_DATE."D;
 
	%IF %SYSFUNC(INTCK(DAY, "&amp;START_DATE."D, "&amp;END_DATE."D, D)) &gt; 1 %THEN %DO;
		DIFF_GR_1_DAY = "YES";
	%END;
	%ELSE %DO;
		DIFF_GR_1_DAY = "NO";
	%END;
 
	%IF %SYSFUNC(INTCK(MONTH, "&amp;START_DATE."D, "&amp;END_DATE."D, D)) &gt; 1 %THEN %DO;
		DIFF_GR_1_MONTH = "YES";
	%END;
	%ELSE %DO;
		DIFF_GR_1_MONTH = "NO";
	%END;
 
	%IF %SYSFUNC(INTCK(YEAR, "&amp;START_DATE."D, "&amp;END_DATE."D, D)) &gt; 1 %THEN %DO;
		DIFF_GR_1_YEAR = "YES";
	%END;
	%ELSE %DO;
		DIFF_GR_1_YEAR = "NO";
	%END;
 
	FORMAT START END DATE9.;
 
	LABEL DIFF_GR_1_DAY = "Difference &gt; 1 day"
		DIFF_GR_1_MONTH = "Difference &gt; 1 month"
		DIFF_GR_1_YEAR = "Difference &gt; 1 year";
RUN;
%MEND;
%DIFFERENCE_MACRO(START_DATE=1JUL2020, END_DATE=30SEP2020);
 
PROC PRINT DATA=WORK.DS_DIFF_MACRO LABEL;
RUN;
SAS INTCK & Macro Example Code

2 thoughts on “Complete Guide for SAS INTCK Function – Many Examples

Comments are closed.