is an integer representing
the difference between any two dates or times or datetimes. Date durations
are integer values representing the difference, in the number of days,
between two SAS dates. Time durations are decimal values representing
the number of seconds between two times or datetimes.

Tip

Date and datetimes durations
can be easily calculated by subtracting the smaller date or datetime
from the larger. When dealing with SAS times, special care must be
taken if the beginning and the end of a duration are on different
calendar days. Whenever possible, the simplest solution is to use
datetimes rather than times.
is a unit of measurement
that SAS can count within an elapsed period of time, such as DAYS,
MONTHS, or HOURS. SAS determines date and time intervals based on
fixed points on the calendar, the clock, or both. The starting point
of an interval calculation defaults to the beginning of the period
in which the beginning value falls, which might not be the actual
beginning value specified. For example, if you are using the INTCK
function to count the months between two dates, regardless of the
actual day of the month specified by the date in the beginning value,
SAS treats it as the first of that month.

SAS provides date, time, and datetime intervals for
counting different periods of elapsed time. You can create multiples
of the intervals and shift their starting point. Use them with the
INTCK and INTNX functions and with procedures that support numbered
lists (such as the PLOT procedure). This is the form of an interval:

name<multiple><.starting-point>

The terms in an interval
have the following definitions:

creates a multiple
of the interval. multiple can
be any positive number. The default is 1. For example, YEAR2 indicates
a two-year interval.

is the starting point
of the interval. By default, the starting point is 1. A value greater
than 1 shifts the start to a later point within the interval. The
unit for shifting depends on the interval, as shown in the following
table. For example, YEAR.3 specifies a yearly period from the first
of March through the end of February of the following year.

This program reads the project start and end dates.
Then, the program calculates the duration between them.

data projects; input Projid @5 startdate date9. @15 enddate date9.; Duration=enddate-startdate; datalines; 398 17oct1997 02nov1997 942 22jan1998 11mar1998 167 15dec1999 15feb2000 250 04jan2001 11jan2001 ; proc print data=projects; format startdate enddate date9.; title 'Days Between Project Start and Project End'; run;

SAS associates date and time intervals
with fixed points on the calendar. For example, the MONTH interval
represents the time from the beginning of one calendar month to the
next, not a period of 30 or 31 days. When you use date and time intervals
(for example, with the INTCK or INTNX functions), SAS bases its calculations
on the calendar divisions that are present. Consider the following
examples:

Multi-unit intervals,
such as MONTH2 or DAY50, also depend on calendar measures, but they
introduce a new problem: SAS can find the beginning of a unit (for
example, the first of a month), but where does that unit fall in the
interval? For example, does the first of October mark the first or
the second month in a two-month interval?

For all multi-unit intervals
except multi-week intervals, SAS creates an interval beginning on
January 1, 1960, and counts forward from that date to determine where
individual intervals begin on the calendar. As a practical matter,
when a year can be divided evenly by an interval, think of the intervals
as beginning with the current year. Thus, MONTH2 intervals begin with
January, March, May, July, September, and November. Consider this
example:

In the above example,
SAS counts 50 days beginning with January 1, 1960; then another 50
days; and so on. As part of this count, SAS counts one DAY50 interval
between October 1, 1998, and January 1, 1999. For example, to determine
the date on which the next DAY50 interval begins, use the INTNX function,
as follows:

Multi-week intervals, such as WEEK2, present a special
case. In general, weekly intervals begin on Sunday, and SAS counts
a week whenever it passes a Sunday. However, SAS cannot calculate
multi-week intervals based on January 1, 1960, because that date fell
on a Friday, as shown:

Therefore, SAS begins
the first interval on Sunday of the week containing January 1, 1960—that
is, on Sunday, December 27, 1959. SAS counts multi-week intervals
from that point. The following example counts the number of two-week
intervals in the month of August 1998:

Shifting the beginning point of
an interval is useful when you want to make the interval represent
a period in your data. For example, if your company's fiscal year
begins on July 1, you can create a year beginning in July by specifying
the YEAR.7 interval. Similarly, you can create a period matching U.S.
presidential elections by specifying the YEAR4.11 interval. This section
discusses how to use shifted intervals and how SAS creates them.

When you shift a time
interval by a subperiod, the shift value must be less than or equal
to the number of subperiods in the interval. For example, YEAR.12
is valid (yearly periods beginning in December), but YEAR.13 is not.
Similarly, YEAR2.25 is not valid because there is no twenty-fifth
month in the two-year period.

In addition, you cannot
shift an interval by itself. For example, you cannot shift the interval
MONTH because the shifting subperiod for MONTH is one month and MONTH
contains only one monthly subperiod. However, you can shift multi-unit
intervals by the subperiod. For example, MONTH2.2 specifies bimonthly
periods starting on the first day of the second month.

For all intervals except
those based on weeks, SAS creates shifted intervals by creating the
interval based on January 1, 1960, by moving forward the required
number of subperiods, and by counting shifted intervals from that
point. For example, suppose you create a shifted interval called DAY50.5.
SAS creates a 50-day interval in which January 1, 1960, is day 1.
SAS then moves forward to day 5. (Note that the difference, or amount
of movement, is four days.) SAS begins counting shifted intervals
from that point. The INTNX function demonstrates that the next interval
begins on January 5, 1960:

For shifted intervals
based on weeks, SAS first creates an interval based on Sunday of the
week containing January 1, 1960 (that is, December 27, 1959). Then,
it moves forward the required number of days. For example, suppose
you want to create the interval WEEK2.8 (biweekly periods beginning
on the second Sunday of the period). SAS measures a two-week interval
based on Sunday of the week containing January 1, 1960, and begins
counting shifted intervals on the eighth day of that. The INTNX function
shows the beginning of the next interval:

You can define custom intervals and associate interval
data sets with new interval names when you use the INTERVALDS= system
option. The dates for these intervals are located in a SAS data set
that you create. The data set must contain two variables, Begin and
End. Each observation represents one interval with the BEGIN variable
that contains the start of the interval and the END variable that
contains the end of the interval. After the custom intervals have
been defined, you can use them with the INTCK and INTNX functions
just as you would use standard 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, and 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. Retail
calendar intervals facilitate comparisons across years, because week
definitions remain consistent from year to year.

The intervals that are
created from the formats can be used in any of the following functions:
INTCINDEX, INTCK, INTCYCLE, INTFIT, INTFMT, INTGET, INTINDEX, INTNX,
INTSEAS, INTSHIFT, and INTTEST.

The following table
lists calendar intervals that are used in the retail industry and
that are ISO 8601 compliant.

Calendar Intervals Used in the Retail Industry