Previous Page | Next Page

Dates, Times, and Intervals

About Date and Time Intervals


Definitions

duration

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.
interval

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.


Syntax

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). The form of an interval is

name<multiple><.starting-point>

The terms in an interval have the following definitions:

name

is the name of the interval. See the following table for a list of intervals and their definitions.

multiple

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

.starting-point

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.


Intervals By Category

Intervals Used with Date and Time Functions
Category Interval Definition Default Starting Point Shift Period Example Description
Date DAY Daily intervals Each day Days DAY3 Three-day intervals starting on Sunday

WEEK Weekly intervals of seven days Each Sunday Days (1=Sunday ... 7=Saturday) WEEK.7 Weekly with Saturday as the first day of the week

WEEKDAY

<daysW>

Daily intervals with Friday-Saturday-Sunday Each day Days WEEKDAY1W Six-day week with Sunday as a weekend day


counted as the same day (five-day work week with a Saturday-Sunday weekend). Days identifies the weekend days by number (1=Sunday ... 7=Saturday). By default, days=17.

WEEKDAY35W Five-day week with Tuesday and Thursday as weekend days (W indicates that day 3 and day 5 are weekend days)

TENDAY Ten-day intervals (a U.S. automobile industry convention) First, eleventh, and twenty-first of each month Ten-day periods TENDAY4.2 Four ten-day periods starting at the second TENDAY period

SEMIMONTH Half-month intervals First and sixteenth of each month Semi-monthly periods SEMIMONTH2.2 Intervals from the sixteenth of one month through the fifteenth of the next month

MONTH Monthly intervals First of each month Months MONTH2.2 February-March, April-May, June-July, August-September, October-November, and December-January of the following year

QTR Quarterly (three-month) intervals January 1 Months QTR3.2 Three-month intervals starting on April 1, July 1, October 1, and January 1
April 1
July 1
October 1

SEMIYEAR Semiannual (six-month) intervals January 1 Months SEMIYEAR.3 Six-month intervals, March-August and September-February
July 1

YEAR Yearly intervals January 1 Months

Datetime Add DT to any of the date intervals Interval corresponding to the associated date interval Midnight of January 1, 1960
DTMONTH
DTWEEKDAY
Time SECOND Second intervals Start of the day (midnight) Seconds


MINUTE Minute intervals Start of the day (midnight) Minutes


HOUR Hourly intervals Start of the day (midnight) Hours


Example: Calculating a Duration

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

data projects;
options nodate pageno=1 linesize=80 pagesize=60;    
   input Projid startdate date9. enddate date9.;
   Duration=enddate-startdate;
   datalines;
398 17oct1997 02nov1997
942 22jan1998 10mar1998
167 15dec1999 15feb2000
250 04jan2001 11jan2001
;

proc print data=projects;
   format startdate enddate date9.;
      title 'Days Between Project Start and Project End';
run;

Output from the PRINT Procedure

             Days Between Project Start and Project End run                8

            Obs    Projid    Startdate      Enddate    Duration

             1       398       17OCT1997  02NOV1997       16   
             2       942       22JAN1998  10MAR1998       47   
             3       167       15DEC1999  15FEB2000       62   
             4       250       04JAN2001  11JAN2001        7   

Boundaries of Intervals

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:

Using INTCK And INTNX
Example Results Explanation
mnthnum1=intck(
'month', '25aug2000'd,
'05sep2000'd);
mnthnum1=1 The number of MONTH intervals the INTCK function counts depends on whether the first day of a month falls within the period.
mnthnum2=intck(
'month', '01aug2000'd,
'31aug2000'd);
mnthnum2=0
next=intnx('month',
'25aug2000'd,1);
next represents 01sep2000 The INTNX function produces the SAS date value that corresponds to the beginning of the next interval.

Note:   The only intervals that do not begin on the same date in each year are WEEK and WEEKDAY. A Sunday can occur on any date because the year is not divided evenly into weeks.  [cautionend]


Single-Unit Intervals

Single-unit intervals begin at the following points on the calendar:

Single-Unit Intervals
Single-Unit Interval Beginning Point on the Calendar
DAY each day
WEEKDAY for a standard weekday:

Start day-End day

Monday-Monday

Tuesday-Tuesday

Wednesday-Wednesday

Thursday-Thursday

Friday-Sunday

WEEK each Sunday
TENDAY the first, eleventh, and twenty-first of each month
SEMIMONTH the first and sixteenth of each month
MONTH the first of each month
QTR the first of January, April, July, and October
SEMIYEAR the first of January and July
YEAR the first of January

Single-unit time intervals begin as follows:

Single-Unit Time Intervals
Single-Unit Time Intervals Beginning Point
SECOND each second
MINUTE each minute
HOUR each hour


Multi-Unit Intervals


Multi-Unit Intervals Other Than Multi-Week Intervals

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:

Month2 Intervals
SAS statements Results
howmany1=intck('month2','15feb2000'd,
'15mar2000'd);
howmany1=1
count=intck('day50','01oct1998'd,
'01jan1999'd); 

count=1

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. As an example, to determine the date on which the next DAY50 interval begins, use the INTNX function, as follows:

Using the INTNX Function
SAS statements Results
start=intnx('day50','01oct98'd,1); 
SAS date value 14200, or Nov 17, 1998

The next interval begins on November 17, 1998.

Time intervals (those that represent divisions of a day) are aligned with the start of the day, that is, midnight. For example, HOUR8 intervals divide the day into the periods 00:00 to 08:00, 8:00 to 16:00, and 16:00 to 24:00 (the next midnight).


Multi-Week Intervals

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:

Calculating Multi-Week Intervals

[Calculating Multi-Week Intervals]

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:
Counting Two-Week Intervals
SAS Statements Results
count=intck('week2','01aug98'D, '31aug98'D); 
count=3

To see the beginning date of the next interval, use the INTNX function, as shown here:

Using INTNX to See the Beginning Date of an Interval
SAS Statements Results
begin=intnx('week2','01aug1998'd,1);
"Begin" represents SAS date 14093 or August 02, 1998

The next interval begins on August 16.


Shifted Intervals

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.


How to Use Shifted Intervals

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.


How SAS Creates Shifted Intervals

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:

Using INTNX to Determine When an Interval Begins
SAS Statements Results
start=intnx('day50.5','01jan1960'd,1);
SAS date value 4, or Jan 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 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:

Using the INTNX Function to Show the Beginning of the Next Interval
SAS Statements Results
start=intnx('week2.8','01jan1960'd,1);   
SAS date value 2, or Jan 3, 1960

You can also shift time intervals. For example, HOUR8.7 intervals divide the day into the periods 06:00 to 14:00, 14:00 to 22:00, and 22:00 to 06:00.


Custom Intervals

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 INTERVALDS system option enables you to increase the number of allowable intervals. In addition to the standard list of intervals (DAY, WEEKDAY, and so on), the names that are listed in INTERVALDS are valid as well.


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, 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
Interval Description
YEARV specifies ISO 8601 yearly intervals. The ISO 8601 year begins on the Monday on or immediately preceding January 4. Note that it is possible for the ISO 8601 year to begin in December of the preceding year. Also, some ISO 8601 years contain a leap week. The beginning subperiod s is written in ISO 8601 weeks (WEEKV).
R445YR is the same as YEARV except that in the retail industry the beginning subperiod s is 4-4-5 months (R445MON).
R454YR is the same as YEARV except that in the retail industry the beginning subperiod s is 4-5-4 months (R454MON).
R544YR is the same as YEARV except that in the retail industry the beginning subperiod s is 5-4-4 months (R544MON).
R445QTR specifies retail 4-4-5 quarterly intervals (every 13 ISO 8601 weeks). Some fourth quarters will contain a leap week. The beginning subperiod s is 4-4-5 months (R445MON).
R454QTR specifies retail 4-5-4 quarterly intervals (every 13 ISO 8601 weeks). Some fourth quarters will contain a leap week. The beginning subperiod s is 4-5-4 months (R454MON).
R544QTR specifies retail 5-4-4 quarterly intervals (every 13 ISO 8601 weeks). Some fourth quarters will contain a leap week. The beginning subperiod s is 5-4-4 months (R544MON).
R445MON specifies retail 4-4-5 monthly intervals. The 3rd, 6th, 9th, and 12th months are five ISO 8601 weeks long with the exception that some 12th months contain leap weeks. All other months are four ISO 8601 weeks long. R445MON intervals begin with the 1st, 5th, 9th, 14th, 18th, 22nd, 27th, 31st, 35th, 40th, 44th, and 48th weeks of the ISO year. The beginning subperiod s is 4-4-5 months (R445MON).
R454MON specifies retail 4-5-4 monthly intervals. The 2nd, 5th, 8th, and 11th months are five ISO 8601 weeks long with the exception that some 12th months contain leap weeks. R454MON intervals begin with the 1st, 5th, 10th, 14th, 18th, 23rd, 27th, 31st, 36th, 40th, 44th, and 49th weeks of the ISO year. The beginning subperiod s is 4-5-4 months (R454MON).
R544MON specifies retail 5-4-4 monthly intervals. The 1st, 4th, 7th, and 10th months are five ISO 8601 weeks long. All other months are four ISO 8601 weeks long with the exception that some 12th months contain leap weeks. R544MON intervals begin with the 1st, 6th, 10th, 14th, 19th, 23rd, 27th, 32nd, 36th, 40th, 45th, and 49th weeks of the ISO year. The beginning subperiod s is 5-4-4 months (R544MON).
WEEKV specifies ISO 8601 weekly intervals of seven days. Each week begins on Monday. The beginning subperiod s is calculated in days (DAY). Note that WEEKV differs from WEEK in that WEEKV.1 begins on Monday, WEEKV.2 begins on Tuesday, and so on.

Previous Page | Next Page | Top of Page