INTNX Function

Increments a SAS date, time, or datetime value encoded as a DOUBLE, and returns a SAS date, time, or datetime value encoded as a DOUBLE.

Category: Date and Time
Returned data type: DOUBLE

Syntax

Arguments

interval[multiple][.shift-index]

specifies a basic or complex interval. Multipliers and shift indexes can be used with the basic interval names to construct more complex interval specifications. The three parts of the interval name are as follows:

interval

specifies a character constant, a variable, or an expression that contains an interval name such as WEEK, MONTH, or QTR.

Data type CHAR
Note The possible values of interval are listed in Intervals Used with Date and Time Functions in SAS Language Reference: Concepts.
Tip Interval can appear in uppercase or lowercase.
Example YEAR specifies year-based intervals.

multiple

specifies an optional multiplier that sets the interval equal to a multiple of the period of the basic interval type.

Data type INTEGER
See Incrementing Dates and Times By Using Multipliers and By Shifting Intervals in SAS Functions and CALL Routines: Reference for more information.
Example YEAR2 specifies a two-year, or biennial, interval type.

shift-index

specifies an optional shift index that shifts the interval to start at a specified subperiod starting point.

Restrictions The shift index cannot be greater than the number of subperiods in the whole interval. For example, you could use YEAR2.24, but YEAR2.25 would be an error because there is no 25th month in a two-year interval.
If the default shift period is the same as the interval type, then only multiperiod intervals can be shifted with the optional shift index. For example, because MONTH type intervals shift by MONTH subperiods by default, monthly intervals cannot be shifted with the shift index. However, bimonthly intervals can be shifted with the shift index, because there are two MONTH intervals in each MONTH2 interval. For example, the interval name MONTH2.2 specifies bimonthly periods starting on the first day of even-numbered months.
Data type INTEGER
See Incrementing Dates and Times By Using Multipliers and By Shifting Intervals in SAS Functions and CALL Routines: Reference for more information.
Example YEAR.3 specifies yearly periods shifted to start on the first of March of each calendar year and to end in February of the following year.

start-from

specifies an expression that represents a SAS date, time, or datetime value encoded as a DOUBLE and that identifies a starting point.

Data type DOUBLE

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.

Data type INTEGER

'alignment'

controls the position of SAS dates within the interval. You must enclose alignment in quotation marks. Alignment can be one of these values:

BEGINNING

specifies that the returned date or datetime value is aligned to the beginning of the interval.

Alias B

MIDDLE

specifies that the returned date or datetime value is aligned to the midpoint of the interval, which is the average of the beginning and ending alignment values.

Alias M

END

specifies that the returned date or datetime value is aligned to the end of the interval.

Alias E

SAME

specifies that the date that is returned has the same alignment as the input date.

Aliases S
SAMEDAY
See SAME Alignment
Default BEGINNING
Data type CHAR
See Aligning SAS Date Output within Its Intervals

Details

The Basics

The INTNX function increments a date, time, or datetime value by intervals such as DAY, WEEK, QTR, and MINUTE, or a custom interval that you define. 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 SAS date value for the beginning date, time, or datetime value of the interval that you specify in the start-from argument. (To convert the date value to a calendar date, use any valid DS2 date format, such as the DATE9. format.) The following example shows how to determine the date of the start of the week that is six weeks from the week of October 17, 2011.
select put(intnx('week', date'2011-10-17', 6), date9.);
INTNX returns the value 27NOV2011.
For more information about working with date and time intervals, see Date and Time Intervals in SAS Functions and CALL Routines: Reference.

Aligning SAS Date Output within Its Intervals

SAS date values are typically aligned with the beginning of the time interval that is specified with the interval argument.
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.

SAME Alignment

If you use the SAME value of the alignment argument, then INTNX returns the same calendar date after computing the interval increment that you specified. The same calendar date is aligned based on the interval's shift period, not the interval. To view the valid shift periods, see Intervals by Category.
Most of the values of the shift period are equal to their corresponding intervals. The exceptions are the intervals WEEK, WEEKDAY, QTR, SEMIYEAR, YEAR, and their DT counterparts. WEEK and WEEKDAY intervals have a shift period of DAYS; and QTR, SEMIYEAR, and YEAR intervals have a shift period of MONTH. When you use SAME alignment with YEAR, for example, the result is same-day alignment based on MONTH, the interval's shift period. The result is not aligned to the same day of the YEAR interval. If you specify a multiple interval, then the default shift interval is based on the interval, and not on the multiple interval.
When you use SAME alignment for QTR, SEMIYEAR, and YEAR intervals, the computed date is the same number of months from the beginning of the interval as the input date. The day of the month matches as closely as possible. Because not all months have the same number of days, it is not always possible to match the day of the month.
For more information about shift periods, see Intervals by Category.

Alignment Intervals

Use the SAME value of the alignment argument if you want to base the alignment of the computed date on the alignment of the input date.
/*** returns 22MAR2011 ***/
select put(intnx('week', date'2011-03-15', 1, 'same'), date9.);

/*** returns 15MAR2016 ***/
select put(intnx('year', date'2011-03-15', 5, 'same'), date9.);

Adjusting Dates

The INTNX function automatically adjusts for the date if the date in the interval that is incremented does not exist. Here is an example:
/*** returns 15AUG2011 ***/
select put(intnx('month', date'2011-03-15', 5, 'same'), date9.);

/*** returns 28FEB2014 ***/
select put(intnx('year', date'2012-02-29', 2, 'same'), date9.);

/*** returns 30SEP2011 ***/
select put(intnx('month', date'2011-08-31', 1, 'same'), date9.);

/*** returns 01MAR2012 (the 1st day of the 3rd month of the year) ***/
select put(intnx('year', date'2011-03-01', 1, 'same'), date9.); 

/*** returns 29FEB2012 (the 60th day of the year) ***/
select put(intnx('year', date'2011-03-01', 1, 'same', 'day'), date9.);
In the following example, the INTNX function returns the value 01JAN2014, which is the beginning of the year two years from the starting date (29FEB2012).
select put(intnx('year', date'2012-02-29', 2), date9.);
In this example , the INTNX function returns the value 28FEB2014. In this case, the starting date begins in the year 2012, the year is two years later (2014), the month is the same (February), and the date is the 28th, because that is the closest date to the 29th in February 2014.
select put(intnx('year', date'2012-02-29', 2, 'same'), date9.);

Retail Calendar 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, or 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. For more information, see Retail Calendar Intervals: ISO 8601 Compliant in SAS Language Reference: Concepts.

Examples

Example 1: Using the INTNX Function

The following statements illustrate the INTNX function.
Statements
Results
select intnx('year', date'2013-02-05', 3);
select put(intnx('year', date'2013-02-05', 3), date7.);
20454
01Jan16
select intnx('month', date'2013-01-05', 0);
select put(intnx('month', date'2013-01-05', 0), date7.);
19359
01JAN13
select intnx('semiyear', date'2013-01-01', 1);
select put(intnx('semiyear', date'2013-01-01', 1), date7.);
19540
01JUL13
select intnx('month2', date'2012-08-01', -1);
select put(intnx('month2', date'2012-08-01', -1), date7.);
19114
01MAY12
select intnx('semimonth2.2', date'2013-04-01', 4);
select put(intnx('semimonth2.2', date'2013-04-01', 
4), date7.);
19555
16JUL13

Example 2: Using the ALIGNMENT Argument

The following examples show the results of advancing a date by using the optional alignment argument.
Statements
Results
select intnx('month', date'2013-01-01', 5, 'beginning');
select put(intnx('month', date'2013-01-01', 5, 'beginning'),
 date7.);
19510
01JUN13
select intnx('month', date'2013-01-01', 5, 'middle');
select put(intnx('month', date'2013-01-01', 5, 'middle'),
 date7.);
19524
15JUN13
select intnx('month', date'2013-01-01', 5, 'end');
select put(intnx('month', date'2013-01-01', 5, 'end'),
 date7.);
19539
30JUN13
select intnx('month', date'2013-01-01', 5, 'sameday');
select put(intnx('month', date'2013-01-01', 5, 'sameday'),
 date7.);
19510
01JUN13
select intnx('month', date'2013-03-15', 5, 'same');
select put(intnx('month', date'2013-03-15', 5, 'same'),
 date7.);
19585
15AUG13

See Also

Functions:
Other References:
Last updated: February 23, 2017