How can I add days to a date in SAS, and can you provide an example? 2

How can I add days to a date in SAS, and can you provide an example?

In SAS, there are several ways to add days to a given date. One method is to use the INTNX function, which allows for the addition of a specified number of days to a date. This function takes in three arguments: the interval, the number of intervals to add, and the starting date. For example, the code “INTNX(‘DAY’, 7, ’01JAN2021′)” will add 7 days to the date ’01JAN2021′ and return the date ’08JAN2021′. This allows for easy manipulation of dates for various analytical purposes.

Add Days to Date in SAS (With Example)


The easiest way to add days to a date variable in SAS is to use the INTNX function.

This function uses the following basic syntax:

INTNX(interval, start_date, increment)

where:

  • interval: The interval to add to date (day, week, month, year, etc.)
  • start_date: Variable that contains start dates
  • increment: The number of intervals to add

The following example shows how to use this syntax in practice.

Example: Add Days to Date in SAS

Suppose we have the following dataset in SAS that shows the total sales made on various days at some store:

/*create dataset*/
data data1;
    input month day year sales;
    datalines;
10 15 2022 45
10 19 2022 50
10 25 2022 39
11 05 2022 14
12 19 2022 29
12 23 2022 40
;
run;

/*create second dataset with date formatted*/
data data2;
  set data1;
  date=mdy(month,day,year);
  format date mmddyy10.;
  drop month day year;
run;

/*view dataset*/
proc printdata=data2;

We can use the following code to create a new column called date_plus5 that adds five days to the values in the date column:

/*create new dataset with column that adds 5 days to date*/
data data3; 
  set data2; 
  date_plus5=intnx('day', date, 5); 
  format date_plus5 mmddyy10.;
run;

/*view dataset*/
proc printdata=data3;

add days to date in SAS

Notice that the new column called date_plus5 contains the values in the date column with fives days added to them.

Note that you can also subtract days by simply using a negative value in the INTNX function.

For example, we can use the following code to subtract five days from each value in the date column:

/*create new dataset with column that subtracts 5 days to date*/
data data3; 
  set data2; 
  date_minus5=intnx('day', date, -5); 
  format date_minus5 mmddyy10.;
run;

/*view dataset*/
proc printdata=data3;

Notice that the new column called dateminus5 contains the values in the date column with fives days subtracted from them.

Note: You can find the complete documentation for the SAS INTNX function .

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I add days to a date in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-sas-and-can-you-provide-an-example/

stats writer. "How can I add days to a date in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-sas-and-can-you-provide-an-example/.

stats writer. "How can I add days to a date in SAS, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-sas-and-can-you-provide-an-example/.

stats writer (2024) 'How can I add days to a date in SAS, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-days-to-a-date-in-sas-and-can-you-provide-an-example/.

[1] stats writer, "How can I add days to a date in SAS, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add days to a date in SAS, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top