INTCK function examples

From sasCommunity
Jump to: navigation, search

This article is about the INTCK function. I believe that the best way to understand how it works is to see some easy examples. Therefore this article contains many of them. Some additional date and time function are also included.


Everyone knows that the INTCK function returns the integer count of the number of interval boundaries between two dates, two times, or two datetime values. (c) SAS Help. In the following Example1 you see how this function works within some basic intervals:

Example1. Date arguments.

years=intck('year','01jan2009'd,'01jan2010'd);
SEMIYEAR=intck('SEMIYEAR','01jan2009'd,'01jan2010'd);
quarters=intck('qtr','01jan2009'd,'01jan2010'd);
months=intck('month','01jan2009'd,'01jan2010'd);
weeks=intck('week','01jan2009'd,'01jan2010'd);
days=intck('day','01jan2009'd,'01jan2010'd);

In the example above we count the difference between two dates: 01-Jan-2009 and 01-Jan-2010 (one year). The result values will be:

  • years = 1
  • semiyears = 2
  • quarters= 4
  • months = 12
  • weeks = 52
  • days = 365

Example2. Datetime arguments.

hours=intck('hour','01jan2009:00:00:00'dt,'01jan2010:00:00:00'dt);
minutes=intck('minute','01jan2009:00:00:00'dt,'01jan2010:00:00:00'dt);
seconds=intck('second','01jan2009:00:00:00'dt,'01jan2010:00:00:00'dt);

The result values will be:

  • hours= 8760
  • minutes= 525600
  • seconds= 31536000

Example3. Time arguments.

hours=intck('hour','00:00:00't,'12:00:00't);
minutes=intck('minute','00:00:00't,'12:00:00't);
seconds=intck('second','00:00:00't,'12:00:00't);

In the example3 (above) we count the difference between two time values: 00:00:00 (midnight) and 12:00:00 (midday). The result values will be:

  • hours= 12
  • minutes= 720
  • seconds= 43200

Important notice! The INTCK functions does not count the number of complete intervals between two values. The example below shows how it calculates the 'YEAR' difference between 31-Dec and 1-Jan:

Example4. Boundaries.

years=intck('year','31dec2009'd,'01jan2010'd);

The result values will be:

  • years= 1

Since you know that it is just ONE DAY and it is not a YEAR the results could be mistakenly considered as incorrect. Please be aware that it returns the integer count of the number of interval boundaries between two dates. In the example above the boundary is the value of '01JAN2010'd that is included (just once) into the interval.

My solution is simple. You just have to use 'day365' instead of 'year'. The 'day365' paremeter tells to the INTCK function to multiply day by 365 times. Look at the next example:

Example5. Days multiplied by 365.

days365=intck('day365','31dec2009'd,'01jan2010'd);

The result values will be:

  • days365 = 0

And that is what we've expected. Actually, you are free to combine it the way you need (e.g. year2 or week4).

Some operations with changing formats are also helpful when you deal with the INTCK function. Please take a look at the next examples. I believe it is quite easy to understand how it works. Example6. Datepart() Timepart() functions.

format a1 b1 date9.;
a0='01jan2009:00:00:00'dt;
b0='01jan2010:00:00:00'dt;
a1=datepart(a0);
b1=datepart(b0);
days=intck('day',a1,b1);

The result value will be:

  • days = 365
format a1 b1 date9.;
a0='01jan2009:00:00:00'dt;
b0='01jan2010:12:00:00'dt;
a1=timepart(a0);
b1=timepart(b0);
hour=intck('hour',a1,b1);

The result value will be:

  • hour= 12

You might find it helpful to use the INTNX function along with the INTCK.


Good luck! Victor Popovich