Increments a date, time, or datetime value by a given interval or intervals, and returns a date, time, or datetime value
Category: Date and Time
INTNX ( interval < multiple >< .shift-index >, start-from , increment <, alignment >)
interval
specifies a character constant, a variable, or an expression that contains a time interval such as WEEK, SEMIYEAR, QTR, or HOUR .
Requirement: The type of interval (date, datetime, or time) must match the type of value in start-from and increment .
See: Table 4.4 on page 605 for a list of commonly used time intervals.
Interval | Description |
---|---|
DAY3 | Three-day intervals starting on Sundays |
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 (with the cycle three-weekday intervals aligned to Monday 4 Jan 1960) |
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 | Three-month intervals starting on April 1, July 1, October 1, and January 1 |
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) |
multiple
specifies a multiple of the interval. It sets the interval equal to a multiple of the interval type. For example, YEAR2 consists of two-year, or biennial, periods.
See: 'Incrementing Dates and Times by Using Multipliers and by Shifting Intervals' on page 604 for more information.
shift-index
specifies the starting point of the interval. By default, the starting point is 1. A value that is greater than 1 shifts the start to a later point within the interval. The unit for shifting depends on the interval. For example, YEAR.3 specifies yearly periods that are shifted to start on the first of March of each calendar year and to end in February of the following year.
Restriction: The shift index cannot be greater than the number of periods in the entire interval. For example, YEAR2.24 has a valid shift index, but YEAR2.25 is invalid because there is no twenty-fifth month in a two-year interval.
Restriction: If the default shift period is the same as the interval type, then you can shift only multiperiod intervals with the shift index. For example, because MONTH type intervals shift by MONTH subperiods by default, you cannot shift monthly intervals with the shift index. However, you can shift bimonthly intervals with the shift index, because two MONTH intervals exist in each MONTH2 interval. The interval name MONTH2.2, for example, specifies bimonthly periods starting on the first day of even-numbered months.
See: 'Incrementing Dates and Times by Using Multipliers and by Shifting Intervals' on page 604 for more information.
start-from
specifies a SAS expression that represents a SAS date, time, or datetime value that identifies a starting point.
increment
specifies a negative, positive, or zero integer that represents the number of date, time, or datetime intervals. Increment is the number of intervals to shift the value of start-from .
alignment
controls the position of SAS dates within the interval. Alignment can be one of these values:
BEGINNING
specifies that the returned date is aligned to the beginning of the interval.
Alias: B
MIDDLE
specifies that the returned date is aligned to the midpoint of the interval.
Alias: M
END
specifies that the returned date is aligned to the end of the interval.
Alias: E
SAMEDAY
specifies that the date that is returned is aligned to the same calendar date with the corresponding interval increment.
Alias: S
Alias: SAME
Default: BEGINNING
See: 'Aligning SAS Date Output within Its Intervals' on page 606 for more information.
The Basics The INTNX function increments a date, time, or datetime value by intervals such as DAY, WEEK, QTR, and MINUTE. The increment is based on a starting date, time, or datetime value, and on the number of time intervals that you specify. The INTNX function returns the beginning date, time, or datetime value of the interval that you specify in the start-from argument. For example, to determine the date of the start of the week that is six weeks from the week of October 17, 2003, use the following example:
intnx('week', '17oct03'd, 6);
INTNX returns the value 23NOV2003.
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.
Understanding 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. The intervals December 1, 1958, November 1, 1957, October 1, 1956, and so on are dates that occurred before the base date of 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.
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 .
SAS date values are normally aligned with the beginning of the time intervals that correspond to the interval type in interval .
You can use the optional alignment argument to specify the alignment of the date that is returned. The values BEGINNING, MIDDLE, or END align the date to the beginning, middle, or end of the interval, respectively.
If you use the SAMEDAY value, then INTNX returns the same calendar date after computing the interval increment that you specified. The function automatically adjusts for the date if the date in the interval that is incremented does not exist. Here is an example:
intnx('month', '15mar2000'd, 5, 'sameday'); returns 15AUG2000 intnx('year', '29feb2000'd, 2, 'sameday'); returns 28FEB2002 intnx('month', '31aug2001'd, 1, 'sameday'); returns 30SEP2001
BEGINNING is the default value for alignment .
The following examples show how to use the INTNX function.
SAS Statements | Results |
---|---|
yr=intnx('year','05feb94'd,3); put yr / yr date7.; | 13515 01JAN97 |
x=intnx('month','05jan95'd,0); put x / x date7.; | 12784 01JAN95 |
next=intnx('semiyear','01jan97'd,1); put next / next date7.; | 13696 01JUL97 |
past=intnx('month2','01aug96'd,-1); put past / past date7.; | 13270 01MAY96 |
sm=intnx('semimonth2.2','01apr97'd,4); put sm / sm date7.; | 13711 16JUL97 |
x='month'; date='1jun1990'd; nextmon=intnx(x,date,1); put nextmon / nextmon date7.; | 11139 01JUL90 |
x1='month '; x2=trim(x1); date='1jun1990'd - 100; nextmonth=intnx(x2,date,1); put nextmonth / nextmonth date7.; | 11017 01MAR90 |
The following examples show the results of advancing a date by using the optional alignment argument.
SAS Statements | Results |
---|---|
date1=intnx('month','01jan95'd,5,'beginning'); put date1 / date1 date7.; | 12935 01JUN95 |
date2=intnx('month','01jan95'd,5,'middle'); put date2 / date2 date7.; | 12949 15JUN95 |
date3=intnx('month','01jan95'd,5,'end'); put date3 / date3 date7.; | 12964 30JUN95 |
date4=intnx('month','01jan95'd,5,'sameday'); put date4 / date4 date7.; | 12935 01JUN95 |
date5=intnx('month','15mar2000'd,5,'same'); put date5 / date5 date9.; | 14837 15AUG2000 |
interval='month'; date='1sep2001'd; align='m'; date4=intnx(interval,date,2,align); put date4 / date4 date7.; | 15294 15NOV01 |
x1='month '; x2=trim(x1); date='1sep2001'd + 90; date5=intnx(x2,date,2,'m'); put date5 / date5 date7.; | 15356 16JAN02 |
Function:
'INTCK Function' on page 598