Hello,
I would like to create a new date variable by adding values (days) to the date in another column. I ran the following code; however, the new column 'END_DATE' shows the same date as the 'ORDER_DATE'
Any help is much appreciated!
Thank you!
data td2;
set td1;
END_DATE=ORDER_DATE + MED_QTY1; /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE DATETIME.;
informat END_DATE ANYDTDTM40.;
run;
That's because end_date (at least) is datetime, not date (seconds, not days, since 1Jan1960).
If you know for sure that order_date is a date variable:
data td2;
set td1;
END_DATE=ORDER_DATE + MED_QTY1; /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE ddmmyy10.;
run;
If it's datetime:
data td2;
set td1;
END_DATE=datepart(ORDER_DATE) + MED_QTY1; /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE ddmmyy10.;
run;
(You don't need the informat)
If, however, you want to keep end_date as datetime, you need to modify med_qty1.
data td2;
set td1;
END_DATE=ORDER_DATE + (MED_QTY1 * 86400); /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE DATETIME.;
run;
There are 86,400 seconds in a day.
That's because end_date (at least) is datetime, not date (seconds, not days, since 1Jan1960).
If you know for sure that order_date is a date variable:
data td2;
set td1;
END_DATE=ORDER_DATE + MED_QTY1; /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE ddmmyy10.;
run;
If it's datetime:
data td2;
set td1;
END_DATE=datepart(ORDER_DATE) + MED_QTY1; /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE ddmmyy10.;
run;
(You don't need the informat)
If, however, you want to keep end_date as datetime, you need to modify med_qty1.
data td2;
set td1;
END_DATE=ORDER_DATE + (MED_QTY1 * 86400); /*this variable contains days; can be 30, 60, 90 etc */
format END_DATE DATETIME.;
run;
There are 86,400 seconds in a day.
Thank you for your reply. It was very helpful. I modified your code a bit.
What worked was:
date td2;
set td1;
format END_DATE DATE9. ;
END_DATE=datepart(ORDER_DATE) + MED_QTY1;
RUN;
Or use the correct function: If ORDER_DATE is datetime
data td2; set td1; END_DATE=intnx('daydt',ORDER_DATE, MED_QTY1); format END_DATE DATETIME.; informat END_DATE ANYDTDTM40.; run;
the INTNX function has many options for adding or subtracting from date, time and datetime variables and you can indicate the interval to use.
Or if Order_date is a DATE value.
data td2; set td1; END_DATE=intnx('day',ORDER_DATE, MED_QTY1); format END_DATE DATE9.; informat END_DATE date9.; run;
Thank you for your reply.
Hang on - you took my solution, and marked your code as the correct answer? Shurely shum mishtake…
<grin>
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.