Functions and CALL Routines |
Category: | Date and Time |
Syntax |
INTNX(interval<multiple><.shift-index>, start-from, increment<, 'alignment'>) |
INTNX(custom-interval, start-from, increment <, 'alignment'>) |
specifies a character constant, variable, or expression that contains a time interval such as WEEK, SEMIYEAR, QTR, or HOUR. Interval can appear in uppercase or lowercase. The possible values of interval are listed in the Intervals Used with Date and Time Functions table in SAS Language Reference: Concepts.
Requirement: | The type of interval (date, datetime, or time) must match the type of value in start-from and increment. |
See: | Commonly Used Time Intervals for a list of commonly used time intervals. |
Multipliers and shift indexes can be used with the basic interval names to construct more complex interval specifications. The general form of an interval name is as follows:
Here are the three parts of the interval name:
specifies the name of the basic interval type. For example, YEAR specifies yearly intervals.
specifies an optional multiplier that sets the interval equal to a multiple of the period of the basic interval type. For example, the interval YEAR2 consists of two-year, or biennial, periods.
See: | Incrementing Dates and Times by Using Multipliers and by Shifting Intervals for more information. |
specifies an optional shift index that shifts the interval to start at a specified subperiod starting point. For 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.
Restriction: | 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. |
Restriction: | 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, MONTH type intervals shift by MONTH subperiods by default; thus, 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. 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 for more information. |
specifies a SAS expression that represents a SAS date, time, or datetime value that identifies a starting point.
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.
controls the position of SAS dates within the interval. You must enclose alignment in quotation marks. Alignment can be one of these values:
specifies that the returned date or datetime value is aligned to the beginning of the interval.
Alias: | B |
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 |
specifies that the returned date or datetime value is aligned to the end of the interval.
Alias: | E |
specifies that the date that is returned has the same alignment as the input date.
Alias: | S |
Alias: | SAMEDAY |
See: | SAME Alignment for more information. |
Default: | BEGINNING |
See: | Aligning SAS Date Output within Its Intervals for more information. |
Details |
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 SAS date value to a calendar date, use any valid SAS 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, 2003.
x=intnx('week', '17oct03'd, 6); put x date9.;
INTNX returns the value 23NOV2003.
For more information about working with date and time intervals, see Date and Time 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.
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 the Intervals Used with Date and Time Functions table in SAS Language Reference: Concepts.
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 the Intervals Used with Date and Time Functions table in SAS Language Reference: Concepts.
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:
intnx('week', '15mar2000'd, 1, 'same'); returns 22MAR2000 intnx('dtweek', '15mar2000:8:45'dt, 1, 'same'); returns 22MAR00:08:45:00 intnx('year', '15mar2000'd, 5, 'same'); returns 15MAR2005
The INTNX 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, 'same'); returns 15AUG2000 intnx('year', '29feb2000'd, 2, 'same'); returns 28FEB2002 intnx('month', '31aug2001'd, 1, 'same'); returns 30SEP2001 intnx('year', '01mar1999'd, 1, 'same'); returns 01MAR2000 (the first day of the third month of the year) intnx('year', '01mar1999'd, 1, 'same'); returns 29FEB2000 (the 60th day of the year)
In the example intnx('year', '29feb2000'd, 2); , the INTNX function returns the value 01JAN2002, which is the beginning of the year two years from the starting date (2000).
In the example intnx('year', '29feb2000'd, 2, 'same'); , the INTNX function returns the value 28FEB2002. In this case, the starting date begins in the year 2000, the year is two years later (2002), the month is the same (February), and the date is the 28th, because that is the closest date to the 29th in February 2002.
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: ISP 8601 Compliant in SAS Language Reference: Concepts.
Examples |
The following SAS statements produce these results.
The following examples show the results of advancing a date by using the optional alignment argument.
The following example uses the custom-interval form of the INTNX function to increment a date, time, or datetime value by a given time interval.
options intervalds=(weekdaycust=dstest); data dstest; format begin end date9.; begin='01jan2008'd; end='01jan2008'd; output; begin='02jan2008'd; end='02jan2008'd; output; begin='03jan2008'd; end='03jan2008'd; output; begin='04jan2008'd; end='06jan2008'd; output; begin='07jan2008'd; end='07jan2008'd; output; begin='08jan2008'd; end='08jan2008'd; output; begin='09jan2008'd; end='09jan2008'd; output; begin='10jan2008'd; end='10jan2008'd; output; begin='11jan2008'd; end='13jan2008'd; output; begin='14jan2008'd; end='14jan2008'd; output; begin='15jan2008'd; end='15jan2008'd; output; run; data _null_; format start date9. endcustom date9.; start='01jan2008'd; do i=0 to 9; endcustom=intnx('weekdaycust', start, i); put endcustom; end; run;
SAS writes the following output to the log:
01JAN2008 02JAN2008 03JAN2008 04JAN2008 07JAN2008 08JAN2008 09JAN2008 10JAN2008 11JAN2008 14JAN2008
See Also |
| |||||
System Options: |
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.