Thursday, December 11, 2008

Interval: Key concept to INTNX and INTCX

When handling dates and times, we must know two powerful functions: INTNK and INTCK.
The two functions are complementary.
INTNK is for new date value incremented by a specified number of intervals, while INTCK for the number of intervals between two dates.

It is hard for a fresh SAS programmer to understand the two functions because they are not intuitive.
However, as a qualified SAS programmer, it should be a must.

Actually, the key concept behind the two functions is INTERVAL.
If you get it, anything will be simple.

INTNX Demo:
INTNX("year", "31Dec2000"d, 2);

As to "Year" interval, "31Dec2000"d is in the interval between "01Jan2000"d and "31Dec2000"d.
So the new interval should be calculated as follow:
"01Jan2000"d and "31Dec2000"d
+
2
= "01Jan2002"d and "31Dec2002"d.
In the new interval, INTNX return date ‘01Jan2002’d. (Note: the default alignment is beginning).

INTCK Demo:
INTCK("year","31Dec2000"d, "01Jan2001"d);
As to ‘Year’ interval, '31Dec2000'd is in the interval between "01Jan2000"d and "31Dec2000"d’,
While "01Jan2001"d in the interval between "01Jan2001"d and "31Dec2001"d

The number of interval is:
"01Jan2001"d and "31Dec2001"d
-
"01Jan2000"d and "31Dec2000"d’
= 1

So INTCX return number 1.


One more thing:
SAS always focus on improvement of "The Power to DO". (I have modified the logo. Wow.)
With new introduction of alignment value ‘sameday’ in INTNK in SAS 9, many old solutions will step off the stage of history.

No comments: