INTCK Function

Returns the number of interval boundaries of a given kind that lie between two dates, times, or datetime values.

Category: Date and Time

Syntax

Required Arguments

interval

specifies a character constant, a variable, or an expression that contains an interval name. Interval can appear in uppercase or lowercase. The possible values of interval are listed in Intervals Used with Date and Time Functions in SAS Language Reference: Concepts.

The type of interval (date, datetime, or time) must match the type of value in start-date.
Multipliers and shift indexes can be used with the basic interval names to construct more complex interval specifications. The general form of an interval name is as follows:

interval<multiple.shift-index>

The three parts of the interval name are listed below:

interval

specifies the name of the basic interval type. For example, YEAR specifies yearly intervals.

multiple

specifies an optional multiplier that sets the interval equal to a multiple of the period of the basic interval type. For example, the interval YEAR2 consists of two-year, or biennial, periods.

See Incrementing Dates and Times by Using Multipliers and by Shifting Intervals for more information.

custom-interval

specifies a user-defined interval that is defined by a SAS data set. Each observation contains two variables, begin and end.

Requirement You must use the INTERVALDS system option if you use the custom-interval variable.
See Details for more information about custom intervals.

shift-index

specifies an optional shift index that shifts the interval to start at a specified subperiod starting point. For example, YEAR.3 specifies yearly periods shifted to start on the first of March of each calendar year and to end in February of the following year.

Restrictions The shift index cannot be greater than the number of subperiods in the entire interval. For example, you could use YEAR2.24, but YEAR2.25 would be an error because there is no 25th month in a two-year interval.
If the default shift period is the same as the interval type, then only multiperiod intervals can be shifted with the optional shift index. For example, MONTH type intervals shift by MONTH subperiods by default. Thus, monthly intervals cannot be shifted with the shift index. However, bimonthly intervals can be shifted with the shift index, because there are two MONTH intervals in each MONTH2 interval. For example, the interval name MONTH2.2 specifies bimonthly periods starting on the first day of even-numbered months.
See Incrementing Dates and Times by Using Multipliers and by Shifting Intervals for more information.

start-date

specifies a SAS expression that represents the starting SAS date, time, or datetime value.

end-date

specifies a SAS expression that represents the ending SAS date, time, or datetime value.

Optional Argument

'method'

specifies that intervals are counted using either a discrete or a continuous method.

You must enclose method in quotation marks. Method can be one of these values:

CONTINUOUS

specifies that continuous time is measured. The interval is shifted based on the starting date.

The continuous method is useful for calculating anniversaries. For example, you can calculate the number of years married by executing the following program:
data b;
   WeddingDay='14feb2000'd;
   Today=today();
   YearsMarried=INTCK('YEAR',WeddingDay,today(),'C');
   format WeddingDay Today date9.;
run;
proc print data=b;
run;
The results are WeddingDay=14FEB2000, Today=17NOV2010, and YearsMarried=10.
For the CONTINUOUS method, the distance in months between January 15, 2000, and February 15, 2000, is one month.
Alias C or CONT

DISCRETE

specifies that discrete time is measured. The discrete method counts interval boundaries (for example, end of month).

The default discrete method is useful to sort time series observations into bins for processing. For example, daily data can be accumulated to monthly data for processing as a monthly series.
For the DISCRETE method, the distance in months between January 31, 2000, and February 1, 2000, is one month.
Alias D or DISC
Default DISCRETE

Details

Calendar Interval Calculations

All values within a discrete time interval are interpreted as being equivalent. This means that the dates of January 1, 2005 and January 15, 2005 are equivalent when you specify a monthly interval. Both of these dates represent the interval that begins on January 1, 2005 and ends on January 31, 2005. You can use the date for the beginning of the interval (January 1, 2005) or the date for the end of the interval (January 31, 2005) to identify the interval. These dates represent all of the dates within the monthly interval.
In the example intck('qtr','14JAN2005'd,'02SEP2005'd);, the start-date ('14JAN2005'd) is equivalent to the first quarter of 2005. The end-date ('02SEP2005'd) is equivalent to the third quarter of 2005. The interval count, that is, the number of times the beginning of an interval is reached in moving from the start-date to the end-date is 2.
The INTCK function using the default discrete method counts the number of times the beginning of an interval is reached in moving from the first date to the second. It does not count the number of complete intervals between two dates:
  • The function INTCK('MONTH','1jan1991'd,'31jan1991'd) returns 0, because the two dates are within the same month.
  • The function INTCK('MONTH','31jan1991'd,'1feb1991’d) returns 1, because the two dates lie in different months that are one month apart.
  • The function INTCK('MONTH','1feb1991'd,'31jan1991'd) returns –1 because the first date is in a later discrete interval than the second date. (INTCK returns a negative value whenever the first date is later than the second date and the two dates are not in the same discrete interval.)
Using the discrete method, WEEK intervals are determined by the number of Sundays, the default first day of the week, that occur between the start-date and the end-date, and not by how many seven-day periods fall between those dates. To count the number of seven-day periods between start-date and end-date, use the continuous method.
Both the multiple and the shift-index arguments are optional and default to 1. For example, YEAR, YEAR1, YEAR.1, and YEAR1.1 are all equivalent ways of specifying ordinary calendar years.
For more information about working with date and time intervals, see Date and Time Intervals.

Date and Datetime Intervals

The intervals that you need to use with SAS datetime values are SAS datetime intervals. Datetime intervals are formed by adding the prefix "DT" to any date interval. For example, MONTH is a SAS date interval, and DTMONTH is a SAS datetime interval. Similarly, YEAR is a SAS date interval, and DTYEAR is a SAS datetime interval.
To ensure correct results with interval functions, use date intervals with date values and datetime intervals with datetime values. SAS does not return an error message if you use a date value with a datetime interval, but the results are incorrect.
The following example uses the DTDAY datetime interval and returns the number of days between August 1, 2011, and February 1, 2012:
data _null_;
   days=intck('dtday', '01aug2011:00:10:48'dt, '01feb2012:00:10:48'dt);
   put days=;
run;
SAS writes the following output to the log:
days=184

Custom Time Intervals

A custom time interval is defined by a SAS data set. The data set must contain the begin variable; it can also contain the end and season variables. Each observation represents one interval with the begin variable containing the start of the interval, and the end variable, if present, containing the end of the interval. The intervals must be listed in ascending order. There cannot be gaps between intervals, and intervals cannot overlap.
The SAS system option INTERVALDS= is used to define custom intervals and associate interval data sets with new interval names. The following example shows how to specify the INTERVALDS= system option:
options intervalds=(interval=libref.dataset-name);
Arguments
interval
specifies the name of an interval. The value of interval is the data set that is named in libref.dataset-name.
libref.dataset-name
specifies the libref and data set name of the file that contains user-supplied holidays.
For more information, see Custom Time Intervals.

Retail Calendar Intervals

The retail industry often accounts for its data by dividing the yearly calendar into four 13-week periods, based on one of the following formats: 4-4-5, 4-5-4, or 5-4-4. The first, second, and third numbers specify the number of weeks in the first, second, and third month of each period, respectively. For more information, see Retail Calendar Intervals: ISO 8601 Compliant in SAS Language Reference: Concepts .

Examples

Example 1: Interval Examples Using INTCK

The following SAS statements produce these results.
SAS Statement
Result
qtr=intck('qtr','10jan95'd,'01jul95'd);
put qtr;
 
2
year=intck('year','31dec94'd,
     '01jan95'd);
put year;
 
1
year=intck('year','01jan94'd,
     '31dec94'd);
put year;
 
0
semi=intck('semiyear','01jan95'd,
     '01jan98'd);
put semi;
 
6
weekvar=intck('week2.2','01jan97'd,
     '31mar97'd);
put weekvar;
 
7
wdvar=intck('weekday7w','01jan97'd,
     '01feb97'd);
put wdvar;
 
26
y='year';
date1='1sep1991'd;
date2='1sep2001'd;
newyears=intck(y,date1,date2);
put newyears;
10
y=trim('year     ');
date1='1sep1991'd + 300;
date2='1sep2001'd - 300;
newyears=intck(y,date1,date2);
put newyears;
8
In the second example, INTCK returns a value of 1 even though only one day has elapsed. This result is returned because the interval from December 31, 1994, to January 1, 1995, contains the starting point for the YEAR interval. However, in the third example, a value of 0 is returned even though 364 days have elapsed. This result is because the period between January 1, 1994, and December 31, 1994, does not contain the starting point for the interval.
In the fourth example, SAS returns a value of 6 because January 1, 1995, through January 1, 1998, contains six semiyearly intervals. (Note that if the ending date were December 31, 1997, SAS would count five intervals.) In the fifth example, SAS returns a value of 6 because there are six two-week intervals beginning on a first Monday during the period of January 1, 1997, through March 31, 1997. In the sixth example, SAS returns the value 26. That indicates that beginning with January 1, 1997, and counting only Saturdays as weekend days through February 1, 1997, the period contains 26 weekdays.
In the seventh example, the use of variables for the arguments is illustrated. The use of expressions for the arguments is illustrated in the last example.

Example 2: An Example That Compares Methods

The following example shows different values for method:
data a;
   interval='month';
   start='14FEB2000'd;
   end='13MAR2000'd;
   months_default=intck(interval, start, end);
   months_discrete=intck(interval, start, end,'d');
   months_continuous=intck(interval, start, end,'c');
   output;
   
   end='14MAR2000'd;
   months_default=intck(interval, start, end);
   months_discrete=intck(interval, start, end,'d');
   months_continuous=intck(interval, start, end,'c');
   output;

   start='31JAN2000'd;
   end='01FEB2000'd;
   months_default=intck(interval, start, end);
   months_discrete=intck(interval, start, end,'d');
   months_continuous=intck(interval, start, end,'c');
   output;
   format start end date.;
run;

proc print data=a;
run;
Comparisons among Methods
Comparisons among Methods

See Also

Functions:
System Options:
INTERVALDS= System Option in SAS System Options: Reference