Besides the INTCK function, one of the most Googled functions in SAS is the INTNX function. In this article, we present a Complete Guide for the SAS INTNX Function with many examples. If you know how to use this function properly, it will save you a lot of coding.

We start with the general syntax of the INTNX function. Then, we divide the remainder of the article into sections focused on how to use the INTNX function for Days, Week, Months, etc. We answer questions such as how to find the next day or how to calculate the first day of next month.

Contents

## The INTNX Syntax

**INTNX**(*interval, start date, increment <, alignment>*)

The SAS INTNX function consists of 4 arguments of which 3 are obligatory:

**interval**: a character constant, variable, or expression (in lower or uppercase) that specifies your interval, e.g. “day” or “month”. This page lists all possible intervals.**start-date**: a Date or DateTime expression.**increment**: a negative, positive, or zero constant that represents the number of intervals to shift from the start date.**alignment**(optional): specifies the position of the interval. The following options exist:- BEGINNING (B) (Default): Specifies that the returned date is at the beginning of the interval.
- END (E): Specifies that the returned date is at the end of the interval.
- SAME (S): Specifies that the returned date has the same alignment as the start date.
- MIDDLE (M): Specifies that the returned date is in the middle of the interval.

In all examples below, we will use SAS Date variables. However, the INTNX function also works with DateTime variables. Nevertheless, you need to convert the DateTime variables into Date variables first or use “dtday” instead of “day”, “dtmonth” instead of “month”, etc. for the *interval* argument.

## The INTNX Function: Days

The SAS INTNX function is frequently used to add or substract a number of days from a given date. In this section, we explain how to do this.

You can use the SAS INTNX function to add or subtract a specific number of days from a given date by setting the *interval* argument to “day”. Next, you use the given date as your start date. Finally, with the *increment* argument, you specify the exact number of days you want to add (positive argument) or subtract (negative argument).

Suppose today is Tuesday, 15th of September 2020. In the example, below we calculate the next day, the previous day and the date 5 days from now.

data _null_; next_day = intnx("day", today(), 1); previous_day = intnx("day", today(), -1); add_5_days = intnx("day", today(), 5); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "Next Day: " next_day EURDFWKX28.; put "Previous Day: " previous_day EURDFWKX28.; put "Today +5 Days: " add_5_days EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***Next Day**: *Wednesday, 16 Sep 2020* **Previous Day**: *Monday, 14 Sep 2020* **Today +5 Days**: *Sunday, 20 Sep 2020*

## The INTNX Function: Weeks

In this section, we discuss with examples how to use the SAS INTNX function with weekly intervals.

With the INTNX function, you can determine the next or previous week by setting the *interval* argument to “week”. In the example below, the start date is today (15th of September 2020). The increment arguments are 1 and -1 respectively. For now, we don’t specify the *alignment* argument.

data _null_; next_week = intnx("week", today(), 1); previous_week = intnx("week", today(), -1); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "Next Week: " next_week EURDFWKX28.; put "Previous Week: " previous_week EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020***Next Week**: *Sunday, 20 Sep 2020***Previous Week**: *Sunday, 6 Sep 2020*

You can also use the SAS INTNX function to calculate the first day, the last, or the same day of the week. To do so we need to use the *alignemnt* argument. In the example below we set this argument to “b” to calculate the first day of the current week, “e” to calculate the last day of previous week, and “s” to calculate the same day of the next week. As *increment* argument we use 0, -1, and 1, respectively.

/* Alignment (week starts on Sunday) */ data _null_; first_day_this_week = intnx("week", today(), 0, "b"); last_day_previous_week = intnx("week", today(), -1, "e"); same_day_next_week = intnx("week", today(), 1, "s"); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "First Day This Week: " first_day_this_week EURDFWKX28.; put "Last Day Previous Week: " last_day_previous_week EURDFWKX28.; put "Same Day Next Week: " same_day_next_week EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020* **First Day This Week**: *Sunday, 13 Sep 2020 ***Last Day Previous Week**: *Saturday, 12 Sep 2020* **Same Day Next Week**: *Tuesday, 22 Sep 2020*

### Weeks Starting on Monday

As the two examples above demonstrate, the first day of the week in SAS is Sunday. However, in many situations, it’s common to start a week on Monday. You can set the *interval* argument to “week.2” to let SAS know that a week starts on Monday. Likewise, if you want to start your weeks on Tuesday, you use “week.3”, and so on.

In the example below, we show how to calculate the first day of the current week, the last day of the previous week, and the same day of the next week with weeks starting on Monday.

/* Alignment (week starts on Monday) */ data _null_; first_day_this_week = intnx("week.2", today(), 0, "b"); last_day_previous_week = intnx("week.2", today(), -1, "e"); same_day_next_week = intnx("week.2", today(), 1, "s"); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "First Day This Week: " first_day_this_week EURDFWKX28.; put "Last Day Previous Week: " last_day_previous_week EURDFWKX28.; put "Same Day Next Week: " same_day_next_week EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020* **First Day This Week**: *Monday, 14 Sep 2020* **Last Day Previous Week**: *Sunday, 13 Sep 2020 ***Same Day Next Week**: *Tuesday, 22 Sep 2020*

## The INTNX Function: Months

In this section, we discuss with examples how to use the SAS INTNX function with monthly intervals.

To use the SAS INTNX function with monthly intervals you need to set the *interval* argument to “month”, or “dtmonth” if you work with Date Time variables. Below we show how to calculate the first day of the previous month and the first day of next month.

data _null_; next_month = intnx("month", today(), 1); previous_month = intnx("month", today(), -1); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "Next Month: " next_month EURDFWKX28.; put "Previous Month: " previous_month EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***Next Month**: *Thursday, 1 Oct 2020 ***Previous Month**: *Saturday, 1 Aug 2020*

You can use the SAS INTNX also to determine the last day or first day of the month. In the example below, we use the *alignment* argument to calculate:

- The last day of the previous month
- The first day of the current month
- The same day of the next month.

By changing the *increment* argument you also calcalte the start or end of a month two or three months from now.

data _null_; first_day_this_month = intnx("month", today(), 0, "b"); last_day_previous_month = intnx("month", today(), -1, "e"); same_day_next_month = intnx("month", today(), 1, "s"); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "First Day This Month: " first_day_this_month EURDFWKX28.; put "Last Day Previous Month: " last_day_previous_month EURDFWKX28.; put "Same Day Next Month: " same_day_next_month EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***First Day This Month**: *Tuesday, 1 Sep 2020 ***Last Day Previous Month**: *Monday, 31 Aug 2020 ***Same Day Next Month**: *Thursday, 15 Oct 2020*

## The INTNX Function: Quarters

In this section, we discuss how to use the INTNX function with quarterly intervals.

You can also use the SAS INTNX function to work with quarters. To do so, you need to set the *interval *argument to “qtr”. In the example below, we determine the first day of the previous quarter and the first day of the next quarter.

data _null_; next_qtr = intnx("qtr", today(), 1); previous_qtr = intnx("qtr", today(), -1); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "Next Quarter: " next_qtr EURDFWKX28.; put "Previous Quarter: " previous_qtr EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***Next Quarter**: *Thursday, 1 Oct 2020 ***Previous Quarter**: *Wednesday, 1 Apr 2020*

You can use the *alignment* argument to let SAS know which day you want from a given quarter. In the example below, we set the *alignment* parameter to “b”, “e”, and “s”, respectively to calculate the first day, last day, and same day of the desired quarters.

data _null_; first_day_this_qtr = intnx("qtr", today(), 0, "b"); last_day_previous_qtr = intnx("qtr", today(), -1, "e"); same_day_next_qtr = intnx("qtr", today(), 1, "s"); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "First Day This Quarter: " first_day_this_qtr EURDFWKX28.; put "Last Day Previous Quarter: " last_day_previous_qtr EURDFWKX28.; put "Same Day Next Quarter: " same_day_next_qtr EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***First Day This Quarter**: *Wednesday, 1 Jul 2020 ***Last Day Previous Quarter**: *Tuesday, 30 Jun 2020 ***Same Day Next Quarter**: *Tuesday, 15 Dec 2020*

## The INTNX Function: Years

Finally, we discuss with examples how to use the SAS INTNX function with yearly intervals.

You can use the INTNX function for calculations with years by setting the *interval* argument to “year”. In the example below, we calculate the first day of the last year and of next year.

data _null_; next_year = intnx("year", today(), 1); previous_year = intnx("year", today(), -1); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "Next Year: " next_year EURDFWKX28.; put "Previous Year: " previous_year EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***Next Year**: *Friday, 1 Jan 2021 ***Previous Year**: *Tuesday, 1 Jan 2019*

You can use the *alignment* argument to specify which day of the year you want. For example, below we calculate the first year of the current year by setting the *increment* argument to zero and the *alignment* argument to “b” (beginning). Also, we calculate the last day of the previous year and the same day of next year.

data _null_; first_day_this_year = intnx("year", today(), 0, "b"); last_day_previous_year = intnx("year", today(), -1, "e"); same_day_next_year = intnx("year", today(), 1, "s"); put "Today: %sysfunc(today(), EURDFWKX28.)"; put "First Day This Year: " first_day_this_year EURDFWKX28.; put "Last Day Previous Year: " last_day_previous_year EURDFWKX28.; put "Same Day Next Year: " same_day_next_year EURDFWKX28.; run;

**Today**: *Tuesday, 15 Sep 2020 ***First Day This Year**: *Wednesday, 1 Jan 2020 ***Last Day Previous Year**: *Tuesday, 31 Dec 2019 ***Same Day Next Year**: *Wednesday, 15 Sep 2021*

## Summary

In this article, we discussed the SAS INTNX function and gave many examples. To summarize this article, the INTNX functions has 4 arguments which you can use for:

**interval:***“day”*: to work with daily intervals*“week”*: to work with weekly intervals*“month”*: to work with monthly intervals*“qtr”*: to work with quarterly intervals*“year”*: to work with quarterly intervals

**start date**: your reference date**increment**:*negative number*: calculate the date*N*days/weeks/months/etc. ago.*positive number*: calculate the date*N*days/weeks/months/etc. from now.*zero*: calculate the date in the same week/month/etc. according to theargument.**alignment**

**alignment**:*“b”*or*“beginning*“: find the beginning / first day of the week/month/qtr/etc.*“e”*or*“end*“: find the end / last day of the week/month/qtr/etc.*“s”*or*“same*“: find the same day of the week/month/qtr/etc.*“m”*or*“middle*“: find the middle day of the week/month/qtr/etc.

## One thought on “Complete Guide for SAS INTNX Function with Examples”

Comments are closed.