Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

SAS : INTNX Function with Examples

$
0
0
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 next tuesday.
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.

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



Viewing all articles
Browse latest Browse all 425

Trending Articles