SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Adding numeric values from another column to date column in SAS

Accepted Solution Solved
Reply
Contributor m_o
Contributor
Posts: 20
Accepted Solution

Adding numeric values from another column to date column in SAS

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;


Accepted Solutions
Solution
‎03-09-2017 08:58 PM
Super Contributor
Posts: 251

Re: Adding numeric values from another column to date column in SAS

[ Edited ]

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


All Replies
Solution
‎03-09-2017 08:58 PM
Super Contributor
Posts: 251

Re: Adding numeric values from another column to date column in SAS

[ Edited ]

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.

Contributor m_o
Contributor
Posts: 20

Re: Adding numeric values from another column to date column in SAS

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;

 

Super User
Posts: 10,536

Re: Adding numeric values from another column to date column in SAS

[ Edited ]

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;

 

 

Contributor m_o
Contributor
Posts: 20

Re: Adding numeric values from another column to date column in SAS

Thank you for your reply. 

Super Contributor
Posts: 251

Re: Adding numeric values from another column to date column in SAS

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

Contributor m_o
Contributor
Posts: 20

Re: Adding numeric values from another column to date column in SAS

Sorry just started learning to use SAS forum today
Super Contributor
Posts: 251

Re: Adding numeric values from another column to date column in SAS

<grin>

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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