Title: The Interval functions in SAS:
1 The Interval functions in SAS INTCK and
INTNX Alan Dickson Independent
Consultant alan.dickson_at_verizon.net (207)
846-8957
2 First, some basics. How does SAS store dates
anyway? SAS stores dates as the number of
days since January 1st, 1960 (This is day
zero). So today - Friday, March 9th 2007 is
stored as 17,234. So, how does SAS store dates
before 1/1/60?
3 So, how does SAS store dates before
1/1/60? Negative numbers! All the way back to
1582 A.D. Why no further back? That was the
year Pope Gregory XIII introduced the current
Gregorian calendar.
4 Why is this storage technique useful? It makes
calculating elapsed days easy! Can you mentally
calculate the number of days between Dec 1, 2007
and Mar 1, 2008 quickly? (Don't forget the leap
year)! SAS can Num_days '01MAR2008'd
'01DEC2007'd Put Num_days N
um_days91
5 Similarly, you can establish future dates based
on current dates with ease. Say your
organization has a 90-day payment window for
accounts receivable. Assuming you know the
invoice date, the rest is straight-forward Due_D
ate Inv_Date 90
6 The INTCK function allows you to determine how
many interval boundaries exist between two
dates. The syntax is Gap INTCK('INTERVAL',
Start_Dt, End_Dt) G1 INTCK('Year',
'01Jan2007'd, '31Dec2008'd) G2 INTCK('Year',
'31Dec2007'd, '01Jan2008'd) Both variables
return the value of 1 only 1 year boundary
crossed.
7The Intervals DAY - each day WEEKDAY -
each weekday (Monday through Friday by
default) WEEK - each Sunday TENDAY
- the 1st, 11th, and 21st of each
month SEMIMONTH - the 1st and 16th of each
month MONTH - the 1st of each month QTR -
the 1st of Jan, Apr, Jul, and Oct SEMIYEAR - the
1st of January and July YEAR - the 1st of
January
8Multi-Unit and Shifted Intervals The basic
intervals can also be modified to have multiple
values and offsets.
Mult Offset V
V Gap INTCK('Year5.2', '01Jan1980'd,
Today()) returns a value of 6 the number of
5-year fiscal boundaries starting in March
between the dates. I.e. March 1 in years '80,
'85, '90, '95, '00 '05.
9The INTNX function allows you to advance (or back
up) a given start-date by however many intervals
you require. The syntax is Result
INTNX('INTERVAL', Start_Dt, Units lt,'Align'gt
) Units can be negative to back up in time. It
can also be zero to manipulate where you are in
the current period. The optional parameter
'Align' defaults to Beginning.
10The Intervals available are exactly as
before. The optional parameter 'Align' was not
available before V8. Several work-arounds were
used. Initially, 'Align' could take on three
values 'Beginning' or 'B' start of
period. 'Middle' or 'M' mid-point of
period. 'End' or 'E' end of period. In
V9.1, a new value of 'Sameday' or 'S' was added.
11An example It is not uncommon in health care to
assume that a patient has coverage throughout any
month that a paid claim is encountered. Cvg_Beg
INTNX('Month', Pd_Dt, 0) Cvg_End
INTNX('Month', Pd_Dt, 1) 1 (The kind of
historical work-around you may find). OR Cvg_End
INTNX('Month', Pd_Dt, 0, 'End')
12Another example You might also find this kind of
historical work-around for the prior lack of
SAMEDAY option. Base_Day DAY(Base_Dt) Tgt_Dt
INTNX('Month', Base_Dt, 6) (Base_Day
1) Now it could be handled as Tgt_Dt
INTNX('Month', Base_Dt, 6, 'S')
13Final example SAS will save you a lot of trouble
if you let it. Tgt_Dt INTNX('Year',
'29FEB2004'd, 3, 'S') Put Tgt_Dt
MMDDYY10. SAS will correctly return Tgt_Dt
02/28/2007 ---------------------------------------
------------------------------ SAS is a
registered trademark of SAS Institute, Cary NC.