BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
m_o
Obsidian | Level 7 m_o
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

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.

View solution in original post

7 REPLIES 7
LaurieF
Barite | Level 11

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.

m_o
Obsidian | Level 7 m_o
Obsidian | Level 7

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;

 

ballardw
Super User

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;

 

 

m_o
Obsidian | Level 7 m_o
Obsidian | Level 7

Thank you for your reply. 

LaurieF
Barite | Level 11

Hang on - you took my solution, and marked your code as the correct answer?  Shurely shum mishtake…

m_o
Obsidian | Level 7 m_o
Obsidian | Level 7
Sorry just started learning to use SAS forum today

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 7 replies
  • 3423 views
  • 2 likes
  • 3 in conversation