The INTNX function is the simplest way to add days to a date in SAS.
The syntax of INTNX function is as follows:
INTNX(interval, start_date, increment)
Below is the explanation of the arguments of this function -
- interval: Time period to add to a date. It can be days, weeks, months, quarters, years.
- start_date: SAS Date Variable
- increment: Number of intervals to add. It can be zero, positive or negative. Negative value refers to previous dates.
See the values of interval in the table below -
Interval | Description |
---|---|
DAY | Add days |
WEEK | Weekly interval of 7 days |
WEEKDAY | Add Weekdays |
SEMIMONTH | Half month interval |
MONTH | Monthly interval |
QTR | Quarterly (3 month) interval |
SEMIYEAR | Half year/Semi-annual (6 month) interval |
YEAR | Yearly interval |
Let's create a sample SAS dataset with two variables containing customers' names and dates. This dataset will be used to demonstrate the INTNX function.
/* Creating a SAS Dataset */ data mydata; input customer $ mydate : date9.; format mydate date9.; datalines; Sam 01JAN2023 Dave 15FEB2023 Donald 10MAR2023 Bill 05APR2023 James 20MAY2023 ; run; /* Print Dataset */ proc print data=mydata; run;
Example 1: Add Days to Date in SAS
Suppose you want to add 10 days to dates in SAS. The following code shows how we can use the INTNX function to add days to a date variable in SAS. In the new dataset named outdata, there is a new date column called newdate with incremented date values.
/* Add 10 days to 'mydate' variable */ data outdata; set mydata; newdate = INTNX('day', mydate, 10); format newdate date9.; run; /* Print Dataset */ proc print data=outdata; run;
Example 2: Add WeekDays to Date in SAS
To add weekdays to a date in SAS, you can specify weekday in the first argument of the INTNX function. The following code creates a new date column called weekdays_5 with 5 weekdays added.
/* Add 5 weekdays to 'mydate' variable */ data outdata; set mydata; weekdays_5 = INTNX('weekday', mydate, 5); format weekdays_5 date9.; run; /* Print Dataset */ proc print data=outdata; run;
Output
As shown in the image below, some dates fall in weekends so difference between dates in "weekdays_5" and "mydate" columns is greater than 5.
Obs customer mydate weekdays_5
1 Sam 01-Jan-23 06-Jan-23
2 Dave 15-Feb-23 22-Feb-23
3 Donald 10-Mar-23 17-Mar-23
4 Bill 05-Apr-23 12-Apr-23
5 James 20-May-23 26-May-23
Example 3: Add Months to Date in SAS
To add months to dates in SAS, we can specify month
in the first argument of INTNX function. In this example, we are creating a new column called newdate that contains dates with 13 months added to mydate variable.
/* Add 13 months to 'mydate' variable */ data outdata; set mydata; newdate = INTNX('month', mydate, 13); format newdate date9.; run; /* Print Dataset */ proc print data=outdata; run;
To subtract days from dates in SAS, we can use a NEGATIVE value in the third argument of the INTNX function. We have created a new date column called date_minus10 that contains dates with 10 days subtracted.
/* Subtract 10 days from 'mydate' variable */ data outdata; set mydata; date_minus10 = INTNX('day', mydate, -10); format date_minus10 date9.; run; /* Print Dataset */ proc print data=outdata; run;
Share Share Tweet