SAS Functions

Increment SAS date with the INTNX function

Increment a SAS date

In SAS you can increment a SAS date with the INTNX function. For example, you can add or extract a day, month, year, etc. from an existing date. Also, the INTNX is very useful to find the next or previous day, month, year, etc. This article describes how you can use the INTNX functions and contains some examples.

The INTNX syntax

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

The INTX contains three obligatory arguments and one optional argument:

  • interval: The name of the interval you use in your increment, e.g. day, month, year. The interval must be in quotation marks. A complete list of intervals can be found here.
  • start: Your date variable.
  • increment: The number of days, months, year, etc. you add/rest to your start date. The increment is a positive or negative integer, or zero.
  • alignment (optional): The position of the date after the increment. The possible increments are: ‘B’ (begin), ‘S’ (same), and ‘E’ (end). The default value is ‘B’.

However, if you have a date-time variable instead of a date variable, your interval must contain ‘dt’. So, ‘dtday’, ‘dtmonth’, ‘dtyear’, etc.

SAS Code & Examples

Below you find several examples and SAS code snippets with the INTNX function. Firstly, a simple SAS date set is create with three dates. Secondly, one day is added to these dates. After that an example follows with the INTNX functions and the optional alignment argument. Finally, some other common use cases are shown.

/* Create a data set with dates */
data work.ds;
	format my_date date9.;
	input my_date date9.;
	datalines;
13JUN2020
05MAY2020
31DEC2019
;
run;
SAS dates
data work.ds_intnx_day;
	set work.ds;
	
	/* Increment by 1 day */
	one_day_incr = intnx('day', my_date, 1);
	
	format _all_ date9.;
run;
SAS INTNX add one day
data work.ds_intnx_month;
	set work.ds;
	
	/* Increment by 1 month */
	one_month_incr = intnx('month', my_date, 1);
	
	/* Increment by 1 month, same day */
	one_month_incr_same = intnx('month', my_date, 1, 's');
	
	/* Increment by 1 month, last day */
	one_month_incr_end = intnx('month', my_date, 1, 'e');

	
	format _all_ date9.;
run;
SAS INTX add month to a date
data work.ds_intnx_other;
	set work.ds;
	
	/* First day of the month */
	first_day_month = intnx('month', my_date, 0);
	
	/* Last day of the month */
	last_day_month = intnx('month', my_date, 0,'e');
	
	/* Current day previous month */
	curr_day_prev_month = intnx('month', my_date, -1, 's');
	
	/* First day of the week */
	first_day_week = intnx('week', my_date, 0);

	
	format _all_ date9.;
run;
SAS INTNX other increments

In the last example, SAS calculates the first day of the current week. In SAS weeks start at sunday.

You can find the official SAS documentation about the INTNX function here.

One thought on “Increment SAS date with the INTNX function

Comments are closed.