# 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.

## What does the INTNX Function do?

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

Examples

Here are some real-world examples of how the INTNX function can be used.

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

## Syntax : INTNX Function

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 Function: Examples

Below is a list of some examples in which we have demonstrated the INTNX function in SAS.

### 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

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 date. 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;```

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 INTNX 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

### 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;```

## Difference between INTNX and INTCK Functions

The INTCK function calculates the difference between two dates or times, whereas the INTNX function adds days or times to a date.

To remember the difference between these two functions easily, focus on the first three letters and the last two letters separately.

• INTCK - INT= Interval CK= Check difference
• INTNX - INT= Interval NX= Next days
Related Posts
Share

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

Post Comment 23 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 ?

1. Adding 's' as the 4th parameter will accomplish this.

data test;
datetime = '29JAN2017:08:34:00'dt;
plus1hour = intnx('hour',datetime,1,'s');
format datetime plus1hour datetime20.;
proc print data=test; run;

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

6. Hi deepanshu,
Thanks a lot for this info.However i have a question , i am trying to run a code that gives me next tuesday ,For 18Mar 2018 is sunday and i want to get 20 March as my answer.I am using tues = intnx ('week',sunday,2); and i am getting 1 April as answer . Any Advise

1. This will do it:

data test;
sunday = '18MAR2018'd;
tues = intnx('week.3',sunday,1);
format sunday tues date9.;
proc print data=test; run;

Your current code finds the beginning of the week 2 weeks from now. The code I suggest gives you the beginning of the week 1 week from now, where each week begins on a Tuesday (day 3).

7. This is a fantastic tutorial. Thank you for providing it! I am troubleshooting another developer's code that contains this function, and this information has been very helpful. Hope it's OK I answered a couple of questions above! I couldn't resist distracting myself from my current work to try out this function. :-)

1. Thank you for stopping by my blog and answering questions. Cheers!

8. 5. Adjustment within the Interval

This program explains how INTCK function adjusts / align dates within the interval specified.

in that we are using intnx ...you have wrote intck thats the mistake ....
website is really fantastic ...

9. HEY: when i m running the code its showing like this.
code:date temp;
mydate='05feb2018'd;
nextsunday=intck('week',mydate,1)
format mydate nextsunday date9.;
run;

output:18sep1951.

1. u mistakenly used intck here.

2. Hlo,u can use the function intck.then the output comes wrong.
u can use intnx function then perfectly u got the o/p.

10. Hi
How to find out next month second Saturday using today's date in SAS

1. You can use below code

Data temp;

Mydate="&SYSDATE"d;
Nxt2ndstdy=intnx('week.7',intnx('month',Mydate,1),2);

Format Mydate Nxt2ndstdy date9.;

Proc print data = temp;

Run;

11. I want more logics in sad

12. I want more logics in sas

13. Obs Date Scd_dep arr_time delay

6210 12/03/2013 8:35:00 PM 8:53:00 PM 18
6211 11/11/2013 2:59:00 PM 2:56:00 PM -3
6212 12/19/2013 8:27:00 PM 8:22:00 PM -5
6213 11/12/2013 8:30:00 AM 10:06:00 AM 96
6214 12/21/2013 2:59:00 PM 3:27:00 PM 28
6215 12/10/2013 8:35:00 PM 8:31:00 PM -4
6216 02/15/2013 3:05:00 PM 3:03:00 PM -2
6217 12/05/2013 8:35:00 PM 12:27:00 AM -1208 ( incorrect)

how to get correct answer for obs no. 6217 ( i am using intck) .How to calculate date if time changes from 23:59 to 00:40 ( next day ).

14. Hi Himanshu,

In 5th point adjustment of interval isn't it intnx function, it's updated as intck function.

15. 2023-06
2023
2023-04
2023-08-12
2023-0
2023-09
.
If the month is=/GT than 6 date should be 31, if the month is LT 5 date should be first of the month, if date is '.' the it should be 2022-12-31, how can we get the answer by using intnx function

Next → ← Prev