SAS : INTNX Function with Examples

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

SAS function INTNX is used to increment SAS date by a specified number of intervals. It helps to answer the following questions.

Examples
1. When is next Monday?
2. When was last Friday?
3. What would be date after 21 weeks?
4. 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])
1. Interval is the unit of measurement. The intervals can be days, weeks, months, quarters, years.
2. Start-from is a SAS date value which would be incremented.
3. Increment is number of intervals by which date is incremented. It can be zero, positive or negative. Negative value refers to previous dates.
4. 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.
data temp;
mydate = '02JAN2017'd;
day=intnx('day', mydate , 7);
format mydate day date9.;
run;

Result : day = 09JAN2017
 SAS INTNX
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.
data temp;
mydate = '02JAN2017'd;
nextsunday=intnx('week', mydate , 1);
format mydate nextsunday date9.;
run;
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.
data temp;
set sashelp.citiday;
firstday=intnx('month', date , 0);
format firstday date9.;
proc print data = temp;
var date firstday;
run;
 SAS : Get First Day
By specifying 0 in the third parameter of INTNX function, we can calculate the first day of the dates.

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 last tuesday.

Incorrect Method
data temp;
mydate = '13JAN2017'd;
lasttuesday = intnx('week.3', mydate , 0);
format mydate lasttuesday date9.;
proc print;
run;
It returns 10JAN2017. In this case, week.3 refers to tuesday within week as a unit of measurement. Similarly, week.2 refers to monday.
It doesn't work when input date is current tuesday. For example, run the above code with mydate = '10JAN2017'd. 10JAN2017 is tuesday. In this case, it returns '10JAN2017' which is not a previous tuesday. It should have returned '03JAN2017'.
Correct Method
data temp;
mydate = '10JAN2017'd;
lasttuesday = intnx('week.4', mydate , -1, 'end');
format mydate lasttuesday date9.;
proc print;
run;
It returns 03JAN2017 which is previous tuesday.  See the changes we have made in this program -

1. -1 instead of 0 as increment value
2. 'end' instead of 'beginning' as date alignment
3. 'week.4' instead of 'week.3' to figure out the last tuesday

5. Adjustment within the Interval

This program explains how INTCK function adjusts / align dates within the interval specified.
data temp;
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.;
proc print;
run;
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.

Result
1. beginning = 01JAN2018
2. middle = 02JUL2018
3. end = 31DEC2018
4. sameday = 31JAN2018
 SAS INTNX Alignment

6. Datetime Formats

Like date formats, we can use time and datetime formats in INTNX function to increment time (seconds / minutes / hours).
data temp;
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;
run;
 SAS Datetime Formats

SAS Tutorials :100 Free SAS Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*
Related Posts:
6 Responses to "SAS : INTNX Function with Examples"
1. the only issue with intnx and intck is that its hard to distinguish to make a short form as other function in sas can be remembered easily.

2. Don't you think that "4. When was Last Tuesday?" will give the current week's Tuesday instead of "Last Tuesday", if you give "09JAN2017" in mydate

1. It works fine for '09JAN2017' which is monday. I guess you meant to say '10JAN2017'. I updated the code. Thanks for pointing it out. Cheers!

3. how to extract last 15 days transaction date wise using this.

4. Hi Deepanshu,
Under "Datetime Formats" heading , when we add hour to the time , it should have been 9:34:00.ideally. How do we achieve this ?

5. Thank you for this high quality introduction to the INTNX function with great examples that are nicely explained and very readable :o)

Next → ← Prev