Previous Page | Next Page

Functions and CALL Routines

Date and Time Intervals


Definition of a Date and Time Interval

An interval is a unit of measurement that SAS counts 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 or clock. 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 that is 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 that is specified by the date in the beginning value, SAS treats the beginning value as the first day of that month.


Interval Names and SAS Dates

Specific interval names are used with SAS date values, while other interval names are used with SAS time and datetime values. The interval names that are used with SAS date values are YEAR, SEMIYEAR, QTR, MONTH, SEMIMONTH, TENDAY, WEEK, WEEKDAY, and DAY. The interval names that are used with SAS time and datetime values are HOUR, MINUTE, and SECOND.

Interval names that are used with SAS date values can be prefixed with 'DT' to construct interval names for use with SAS datetime values. The interval names DTYEAR, DTSEMIYEAR, DTQTR, DTMONTH, DTSEMIMONTH, DTTENDAY, DTWEEK, DTWEEKDAY, and DTDAY are used with SAS time or datetime values.


Incrementing Dates and Times by Using Multipliers and by Shifting Intervals

SAS provides date, time, and datetime intervals for counting different periods of elapsed time. By using multipliers and shift indexes, you can create multiples of intervals and shift their starting point to construct more complex interval specifications.

The general form of an interval name is

name<multiplier><.shift-index>

Both the multiplier 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 that begin in January. If you specify other values for multiplier and for shift-index, you can create multiple intervals that begin in different parts of the year. For example, the interval WEEK6.11 specifies six-week intervals starting on second Wednesdays.

For more information, see Single-unit Intervals, Multi-unit Intervals, and Shifted Intervals in SAS Language Reference: Concepts.


Commonly Used Time Intervals

Time intervals that do not nest within years or days are aligned relative to the SAS date or datetime value 0. SAS uses the arbitrary reference time of midnight on January 1, 1960, as the origin for non-shifted intervals. Shifted intervals are defined relative to January 1, 1960.

For example, MONTH13 defines the intervals January 1, 1960, February 1, 1961, March 1, 1962, and so on, and the intervals December 1, 1958, November 1, 1957, and so on, before the base date January 1, 1960.

As another example, the interval specification WEEK6.13 defines six-week periods starting on second Fridays. The convention of alignment relative to the period that contains January 1, 1960, determines where to start counting to determine which dates correspond to the second Fridays of six-week intervals.

The following table lists time intervals that are commonly used.

Commonly Used Intervals with Optional Multiplier and Shift Indexes
Interval Description
DAY3 Three-day intervals
WEEK Weekly intervals starting on Sundays
WEEK.7 Weekly intervals starting on Saturdays
WEEK6.13 Six-week intervals starting on second Fridays
WEEK2 Biweekly intervals starting on first Sundays
WEEK1.1 Same as WEEK
WEEK.2 Weekly intervals starting on Mondays
WEEK6.3 Six-week intervals starting on first Tuesdays
WEEK6.11 Six-week intervals starting on second Wednesdays
WEEK4 Four-week intervals starting on first Sundays
WEEKDAY Five-day work week with a Saturday-Sunday weekend
WEEKDAY1W Six-day week with Sunday as a weekend day
WEEKDAY35W Five-day week with Tuesday and Thursday as weekend days (W indicates that day 3 and day 5 are weekend days)
WEEKDAY17W Same as WEEKDAY
WEEKDAY67W Five-day week with Friday and Saturday as weekend days
WEEKDAY3.2 Three-weekday intervals with Saturday and Sunday as weekend days (The intervals are aligned with respect to Jan. 1, 1960. For intervals that nest within a year, it is not necessary to go back to Jan. 1, 1960 to determine the alignment.)
TENDAY4.2 Four ten-day periods starting at the second TENDAY period
SEMIMONTH2.2 Intervals from the sixteenth of one month through the fifteenth of the next month
MONTH2.2 February-March, April-May, June-July, August-September, October-November, and December-January of the following year
MONTH2 January-February, March-April, May-June, July-August, September-October, November-December
QTR3.2 Nine-month intervals starting on February 1, 1960, November 1, 1960, August 1, 1961, May 1, 1962, and so on.
SEMIYEAR.3 Six-month intervals, March-August and September-February
YEAR.10 Fiscal years starting in October
YEAR2.7 Biennial intervals starting in July of even years
YEAR2.19 Biennial intervals starting in July of odd years
YEAR4.11 Four-year intervals starting in November of leap years (frequency of U.S. presidential elections)
YEAR4.35 Four-year intervals starting in November of even years between leap years (frequency of U.S. midterm elections)
DTMONTH13 Thirteen-month intervals starting at midnight of January 1, 1960, such as November 1, 1957, December 1, 1958, January 1, 1960, February 1, 1961, and March 1, 1962
HOUR8.7 Eight-hour intervals starting at 6 a.m., 2 p.m., and 10 p.m. (might be used for work shifts)

For a complete list of the valid values for interval, see the Intervals Used with Date and Time Functions table in SAS Language Reference: Concepts.


Retail Calendar Intervals: ISO 8601 Compliant

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 months of each period, respectively.

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.

For more information, see Retail Calendar Intervals: ISO 8601 Compliant in SAS Language Reference: Concepts.


Best Practices for Custom Interval Names

The following items list best practices to use when you are creating custom interval names:

Previous Page | Next Page | Top of Page