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.
Here are some real-world examples of how the INTNX function can be used.
- When is next Monday?
- When was last Friday?
- What would be date after 21 weeks?
- 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])
- 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 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;
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;
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 Methoddata 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 instead of 0 as increment value
- 'end' instead of 'beginning' as date alignment
- '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.
- beginning = 01JAN2018
- middle = 02JUL2018
- end = 31DEC2018
- 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
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.
ReplyDeleteDon'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
ReplyDeleteIt works fine for '09JAN2017' which is monday. I guess you meant to say '10JAN2017'. I updated the code. Thanks for pointing it out. Cheers!
Deletehow to extract last 15 days transaction date wise using this.
ReplyDeleteHi Deepanshu,
ReplyDeleteUnder "Datetime Formats" heading , when we add hour to the time , it should have been 9:34:00.ideally. How do we achieve this ?
Adding 's' as the 4th parameter will accomplish this.
Deletedata test;
datetime = '29JAN2017:08:34:00'dt;
plus1hour = intnx('hour',datetime,1,'s');
format datetime plus1hour datetime20.;
proc print data=test; run;
Thank you for this high quality introduction to the INTNX function with great examples that are nicely explained and very readable :o)
ReplyDeleteHi deepanshu,
ReplyDeleteThanks 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
This will do it:
Deletedata 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).
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. :-)
ReplyDeleteThank you for stopping by my blog and answering questions. Cheers!
Delete5. Adjustment within the Interval
ReplyDeleteThis 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 ...
HEY: when i m running the code its showing like this.
ReplyDeletecode:date temp;
mydate='05feb2018'd;
nextsunday=intck('week',mydate,1)
format mydate nextsunday date9.;
run;
output:18sep1951.
this is the output coming please help me out.
u mistakenly used intck here.
DeleteHlo,u can use the function intck.then the output comes wrong.
Deleteu can use intnx function then perfectly u got the o/p.
Hi
ReplyDeleteHow to find out next month second Saturday using today's date in SAS
You can use below code
DeleteData temp;
Mydate="&SYSDATE"d;
Nxt2ndstdy=intnx('week.7',intnx('month',Mydate,1),2);
Format Mydate Nxt2ndstdy date9.;
Proc print data = temp;
Run;
I want more logics in sad
ReplyDeleteSas
DeleteI want more logics in sas
ReplyDeleteObs Date Scd_dep arr_time delay
ReplyDelete6210 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 ).
Hi Himanshu,
ReplyDeleteIn 5th point adjustment of interval isn't it intnx function, it's updated as intck function.
2023-06
ReplyDelete2023
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