This tutorial explains how SAS INTNX function works. It includes explanation of INTNX function with practical examples which would help you to understand it.
SAS INTNX : Introduction
- When is next Monday?
- When was last Friday?
- What would be date after 21 weeks?
- Subtract 2 quarters from the current date
SAS INTNX Syntax
The first three parameters of the INTNX function is mandatory and the fourth one is optional.
INTNX(interval, start-from, increment, [alignment])
- Interval is the unit of measurement. The intervals can be days, weeks, months, quarters, years.
- Start-from is a SAS date value which would be incremented.
- Increment is number of intervals by which date is incremented. It can be zero, positive or negative. Negative value refers to previous dates.
- Alignment [Optional Parameter] is where datevalue is aligned within interval prior to being incremented. The values you can specify - 'beginning', 'middle', 'end', 'sameday'. Default value - 'beginning'.
INTNX : Examples
1. Add 7 days to a specific date
In the following code, we are adding seven days to 02 January 2017.
mydate = '02JAN2017'd;
day=intnx('day', mydate , 7);
format mydate day date9.;
Result : day = 09JAN2017
If you are wondering how INTNX is different to 'simply adding 7 to mydate variable' like code below. You would get answer to this question in the next example.
day = mydate + 7;
2. Find Next Sunday
In this case, we need to find answer of the question 'when is next sunday?'. The 02January,2017 is Monday.
mydate = '02JAN2017'd;
nextsunday=intnx('week', mydate , 1);
format mydate nextsunday date9.;
Result : nextsunday = 08JAN2017
It returns 08JAN2017 as it aligns to the 'beginning' period. The 'beginning' alignment is default in INTNX function. In other words, if you change the mydate to '04JAN2017'd, it still returns '08JAN2017' as the next sunday would be same within this week interval.
If you want to add exactly 1 week to the date, you can use the 'sameday' in the fourth parameter of this function. See the statement below -
nextsunday=intnx('week', mydate , 1, 'sameday'); returns 09JAN2017
3. Get First Date
Suppose you need to find out the first day of a specific day. For example, today is 09January, 2017 and the first day of this date is 01January,2017.
firstday=intnx('month', date , 0);
format firstday date9.;
proc print data = temp;
var date firstday;
|SAS : Get First Day|
4. When was Last Tuesday?
It is tricky to figure out the date when it was last tuesday. 13January,2017 is Friday. In real world dataset, we don't have the exact days of a list of dates when we need to code to get the next tuesday.
mydate = '13JAN2017'd;
lasttuesday = intnx('week.3', mydate , 0);
format mydate lasttuesday date9.;
It returns 10JAN2017. In this case, week.3 refers to tuesday within week as a unit of measurement. Similarly, week.2 refers to monday.
5. Adjustment within the Interval
This program explains how INTCK function adjusts / align dates within the interval specified.
mydate = '31JAN2017'd;
beginning=intnx('year ', mydate , 1, 'b');
middle=intnx('year ', mydate , 1, 'm');
end=intnx('year ', mydate , 1, 'e');
sameday=intnx('year ', mydate , 1, 's');
format mydate beginning middle end sameday date9.;
The abbreviation 'b' refers to beginning, 'm' - middle, 'e' - end, 's' - sameday. The default value is 'b' if you don't specify anything in the fourth parameter.
6. Datetime Formats
Like date formats, we can use time and datetime formats in INTNX function to increment time (seconds / minutes / hours).
mydt = '29JAN2017:08:34:00'dt;
seconds=intnx('second', mydt , 1);
minutes=intnx('minute', mydt , 1);
hours=intnx('hour', mydt , 1);
days=intnx('dtDay', mydt , 1);
weeks=intnx('dtWeek', mydt , 1);
format mydt seconds minutes hours days weeks datetime20.;
proc print NOOBS;
|SAS Datetime Formats|