DATA Step, Macro, Functions and more

Converting character date to " DATETIME20."

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

Converting character date to " DATETIME20."

Hi SAS users,

 

Need help with generating charcter date like below to DATE format to insert into ORacle Database.

 

I tried the below way in  EG & got it to the default date of SAS than the original date. where i am doing wrong?

 

data test;
date1 = '10/15/2017';

run;

 

 

proc sql;
create table test2 as
select
input(date1, MMDDYY10.) AS date2 format = DATETIME20.
from test
;
QUIT;

 

Thanks,

Ana


Accepted Solutions
Solution
‎02-17-2017 11:15 PM
Super Contributor
Posts: 251

Re: Converting character date to " DATETIME20."

Oh, OK. What happens when you pass it to Oracle once you've done that? As long as the variable's format is set to datetime and the value is in seconds, the Oracle engine should do anything it needs to do for you.

View solution in original post


All Replies
Super Contributor
Posts: 251

Re: Converting character date to " DATETIME20."

You're almost there. You have to convert it from date (days since 1Jan1960) to datetime (seconds from…). The dhms function does this:

data test;
date1 = '10/15/2017';
run;
 
 
proc sql;
create table test2 as
select
dhms(input(date1, MMDDYY10.), 0, 0, 0) AS date2 format = DATETIME20.
from test
;
QUIT;
Frequent Contributor
Posts: 84

Re: Converting character date to " DATETIME20."

Thank you. I tried below ways too.

 

proc sql;
create table test2 as
select
input(date1, MMDDYY10.) * 86400 AS date2 format = DATETIME20. ,
input(date1,anydtdtm20.) AS date3 format = DATETIME. ,
dhms(input(date1, MMDDYY10.), 0, 0, 0) AS date4 format = DATETIME20.
from test
;
QUIT;

 

All date2, date3, date4 were giving me results like -  

15OCT2017:00:00:00          15OCT17:00:00:00          15OCT2017:00:00:00

 

I am looking for "9/20/2015 12:00:00 AM" format .

 

Thanks,

Ana

Solution
‎02-17-2017 11:15 PM
Super Contributor
Posts: 251

Re: Converting character date to " DATETIME20."

Oh, OK. What happens when you pass it to Oracle once you've done that? As long as the variable's format is set to datetime and the value is in seconds, the Oracle engine should do anything it needs to do for you.

Frequent Contributor
Posts: 84

Re: Converting character date to " DATETIME20."

Hi Laurie,

 

You are right. Oracle converted to the format i wanted internally. Thanks for the suggestion.

 

Thanks,

Ana

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 295 views
  • 1 like
  • 2 in conversation