Getting the Most out of the INTNX Function

  • Archive
  • SAS
This is one of our archive tips that we’ve kept as its still popular. Some of the information may be out of date. Get in touch if you need more help.

 

The INTNX function is a SAS date time function that returns a SAS date value by adding a specific number of increments to an initial start date for example, add one week to today to return the date for next week. The syntax for the INTNX function is as follows:

sas_date_value  =  intnx('Interval', start_date, 
			number of intervals to add);

The available intervals are Day, Week, Month, Qtr (quarter) or Year and must be enclosed in quotes. The start date must be a SAS date and the number of intervals must be an integer value. A positive number of intervals will return a date in the future and a negative value a data in the past.

In addition to these three arguments an optional fourth argument can be included. This allows greater control over the exact date returned; the values for the fourth argument are b, e, s and m and must also be enclosed in quotation marks. The effect of adding these values as the fourth argument are listed below:

b – The date of the beginning of the interval is returned (first day of the week/ month/year). This is also the default if nothing is entered.
e- The date of end of the interval is returned (last day of the week/ month/year).
m- The date of the middle of the interval (middle day of the week/ month/year).
s – The date of the same day within the interval is returned (same day of the week/ month/year).

Using all four arguments allows for a greater degree of control over the date to be derived. The code example below provides a number of suggestions on how this function can be applied to derive useful dates relative to the start date, in these example the TODAY() function is used to provide the current day as the start day parameter.

data _null_;
  today= today();
  nextweek  =intnx('week',today(),1);
  lastweek  =intnx('week',today(),-1);
  firstdaynextmonth =intnx('month',today(),1,'b');
  lastdaylastmonth =intnx('month',today(),-1,'e');
  samedaylastmonth =intnx('month',today(),-1,'s');
  middlelastmonth =intnx('month',today(),-1,'m');
  lastdaynextquarter =intnx('qtr',today(),1,'e');
 
 put 'Today is ' today :date9.;
  put 'Next week ' nextweek :date9.;
  put 'LastWeek is ' lastweek :date9.;
  put 'The First Day of Next Month is ' firstdaynextmonth :date9.;
  put 'The Last Day of Last Month is ' lastdaylastmonth :date9.;
  put 'The Same Day of Last Month is ' samedaylastmonth :date9.;
  put 'The Middle of Last Month is ' middlelastmonth :date9.;
  put 'The Last Day of Next Quarter is ' lastdaynextquarter :date9.;
run;

Remember that INTNX can be nested with other functions to provide even more applications, for more information view the SAS help entries or the SAS support website – support.sas.com

 

Back to Insights

Talk to us about how we can help