To add or subtract time from a date in a SAS data step, we can use the SAS intnx() function.

data data_new;
	set data;
        date_plus_1_day = intnx('day', date_variable, 1, 'same');
        date_plus_1_mon = intnx('month', date_variable, 1, 'same');
        date_plus_1_yr = intnx('year', date_variable, 1, 'same');
run;

When working with dates in our data, it’s very useful to be able to easily add or subtract time to our date variables.

The SAS intnx() function allows us to add and subtract time very easily. We can add and subtract time by a given interval (second, minute, hour, day, week, month, year). In addition, we can adjust dates to the beginning or end of an interval if necessary.

From the documentation, the SAS intnx() function takes 3 arguments, ‘interval’, ‘start-form’, and ‘increment’, with an optional fourth argument ‘alignment’.

INTNX(interval, start-from, increment <, 'alignment'>)

The ‘interval’ is the interval you want to add or subtract (seconds, minutes, hours, days, weeks, months, years), ‘start-from’ is the date you will adjust, ‘increment’ is how many intervals you want to add or subtract, and ‘alignment’ is if you want to align the date to the beginning, middle, or end of the interval.

Let’s say we have the following SAS dataset with some date variables.

data data_with_dates;
    input d :date9.;
    format d date9.;
    datalines;
31DEC2021
24OCT2020
12DEC2019   
07JUN2019
17FEB2021
12JAN2021
03MAR2020
;
run;

Let’s add and subtract time from each of these variables – let’s add 1 day, add 3 months, add 2 years, and substract 4 weeks from each of the dates.

data date_new;
    format d date9. d_plus_1_day date9. d_plus_3_mon date9. d_plus_2_yrs date9. d_minus_4_wk date9. ;
    set data_with_dates;
    d_plus_1_day = intnx('day', d, 1, 'same');
    d_plus_3_mon = intnx('month', d, 3, 'same');
    d_plus_2_yrs = intnx('year', d, 2, 'same');
    d_minus_4_wk = intnx('week', d, -4, 'same');
run;

/* Output */
          d d_plus_1_day d_plus_3_mon d_plus_2_yrs d_minus_4_wk
1 31DEC2021    01JAN2022    31MAR2022    31DEC2023    03DEC2021
2 24OCT2020    25OCT2020    24JAN2021    24OCT2022    26SEP2020
3 12DEC2019    13DEC2019    12MAR2020    12DEC2021    14NOV2019
4 07JUN2019    08JUN2019    07SEP2019    07JUN2021    10MAY2019
5 17FEB2021    18FEB2021    17MAY2021    17FEB2023    20JAN2021
6 12JAN2021    13JAN2021    12APR2021    12JAN2023    15DEC2020
7 03MAR2020    04MAR2020    03JUN2020    03MAR2022    04FEB2020

Get Beginning or End of Interval with SAS intnx()

With the SAS intnx(), we can easily adjust dates to the beginning, middle or end of an interval with the ‘alignment’ argument.

Let’s say we have the same dataset as above. Let’s get the beginning of the month for each of the days in the dataset, as well as the end of the month for each date.

The following SAS code and output shows how easy it is to get the beginning and end of the month for each of the dates.

data date_new;
    format d date9. d_mon_beg date9. d_mon_end date9.;
    set data_with_dates;
    d_mon_beg = intnx('month', d, 0, 'begin');
    d_mon_end = intnx('month', d, 0, 'end');
run;

/* Output */
          d d_mon_beg d_mon_end
1 31DEC2021 01DEC2021 31DEC2021
2 24OCT2020 01OCT2020 31OCT2020
3 12DEC2019 01DEC2019 31DEC2019
4 07JUN2019 01JUN2019 30JUN2019
5 17FEB2021 01FEB2021 28FEB2021
6 12JAN2021 01JAN2021 31JAN2021
7 03MAR2020 01MAR2020 31MAR2020

Using SAS intnx() in a Loop to Create New Dates

If you are working as an analyst trying to forecast data with a model, the SAS intnx() function is very useful.

Let’s say we have a simple sales model which projects an increase of 10% in sales each year. Let’s say we have a dataset where we’d like to project out sales for the next 5 years.

We can use our simple sales model to create a forecast dataset very easily with the help of the SAS intnx() function.

data current_sales;
    input d :date9. sales;
    format d date9.;
    datalines;
31DEC2021 100
;
run;

To forecast our data for 5 years, we will use a simple loop and intnx().

data forecast_sales(drop=d i);
    format forecast_date date9.;
    set current_sales;
    do i = 1 to 5;
        forecast_date = intnx("year", d, i,"end");
        sales = sales * 1.1;
        output;
    end;
run;
 
/* Output */
   forecast_date   sales
1      31DEC2022     110	
2      31DEC2023     121	
3      31DEC2024   133.1	
4      31DEC2025  146.41	
5      31DEC2026 161.051

How to Add Days to SAS Date Variable with SAS intnx()

To add days to a SAS date variable, we can use the SAS intnx() function. We pass ‘day’ to the ‘interval’ argument in the intnx() function.

intnx("day", date_variable, number_of_days);

How to Add Weeks to SAS Date Variable with SAS intnx()

To add weeks to a SAS date variable, we can use the SAS intnx() function. We pass ‘week’ to the ‘interval’ argument in the intnx() function.

intnx("week", date_variable, number_of_weeks);

How to Add Months to SAS Date Variable with SAS intnx()

To add months to a SAS date variable, we can use the SAS intnx() function. We pass ‘month’ to the ‘interval’ argument in the intnx() function.

intnx("month", date_variable, number_of_months);

How to Add Quarters to SAS Date Variable with SAS intnx()

To add quarters to a SAS date variable, we can use the SAS intnx() function. We pass ‘qtr’ to the ‘interval’ argument in the intnx() function.

intnx("qtr", date_variable, number of quarters);

How to Add Years to SAS Date Variable with SAS intnx()

To add years to a SAS date variable, we can use the SAS intnx() function. We pass ‘year’ to the ‘interval’ argument in the intnx() function.

intnx("year", date_variable, number_of_years);

Hopefully this article has been useful for you to help you understand how to use the SAS intnx() function to increment the date for SAS date variables.

Categorized in:

SAS,

Last Update: February 26, 2024