DATA Step, Macro, Functions and more

date to datetime conversion

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 109
Accepted Solution

date to datetime conversion

I have a dataset where I have a variable which is in date9 format.

 

I am changing the date to datetime using fomat as following.

 

data proclib.a_e_1;
format AEENDTC DATETIME20. AEENRF $12. AESEV $64. AESR $12. AESTDTC DATETIME20. AETERM $124. COUNTRY $12. SITEID 
$16. STUDYID $36. SUBJID 8.;
set proclib.a_e;
run;

In this Process the dates are getting appended with current time stamp from the system or some random time stamp.
I need them to be 00:00:00.

 

Is there any way of controlling that.

 

Thanks in Advance.


Accepted Solutions
Solution
‎04-23-2018 03:22 AM
Super User
Posts: 9,614

Re: date to datetime conversion

Posted in reply to Satish_Parida

SAS dates are counts of days, SAS datetimes are counts of seconds.

datetime = dhms(date,0,0,0);

will convert a date to a datetime. Or multiply by 86400.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎04-23-2018 03:22 AM
Super User
Posts: 9,614

Re: date to datetime conversion

Posted in reply to Satish_Parida

SAS dates are counts of days, SAS datetimes are counts of seconds.

datetime = dhms(date,0,0,0);

will convert a date to a datetime. Or multiply by 86400.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 109

Re: date to datetime conversion

Posted in reply to KurtBremser

The Problem is the column assignment is dynamic and I do not know the column names the time I write the code.

 

The formats are derived from a source table, which is in oracle so the difference of data and datetime (Oracle always sends dates as datetime)

 

So I can not use dhms in the code as I wont be knowing the variable names.

 

Thank you for quick response. 

Respected Advisor
Posts: 2,662

Re: date to datetime conversion

Posted in reply to Satish_Parida

@Satish_Parida wrote:

The Problem is the column assignment is dynamic and I do not know the column names the time I write the code.

 


But this is an entirely different problem than the one you described originally.

 

It may be that some sort of logic and use of macros will help you solve this. But you'd have to describe the problem in a lot more detail.

--
Paige Miller
Super User
Posts: 13,084

Re: date to datetime conversion

Posted in reply to Satish_Parida

@Satish_Parida wrote:

The Problem is the column assignment is dynamic and I do not know the column names the time I write the code.

 

The formats are derived from a source table, which is in oracle so the difference of data and datetime (Oracle always sends dates as datetime)

 

So I can not use dhms in the code as I wont be knowing the variable names.

 

Thank you for quick response. 


Since Oracle always sends the value as date time where are you getting a date from that needs to be converted back to datetime? This kind of sounds like a somewhat flawed process. I would go back to where you convert the Oracle datetime value to a date and re-examine what you are doing to the data.

Super User
Posts: 9,614

Re: date to datetime conversion

Posted in reply to Satish_Parida

@Satish_Parida wrote:

The Problem is the column assignment is dynamic and I do not know the column names the time I write the code.

 

The formats are derived from a source table, which is in oracle so the difference of data and datetime (Oracle always sends dates as datetime)

 

So I can not use dhms in the code as I wont be knowing the variable names.

 

Thank you for quick response. 


You can't write code without knowing the data. So you either have fixed names, or names supplied by another means (macro variables, call execute off datasets). Your argument is therefore a non-argument.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 109

Re: date to datetime conversion

Posted in reply to KurtBremser

Yes, you are correct. I was trying to find out an easy way out rather than writing very complex macro functions to achieve this.

 

Thank you.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 90 views
  • 0 likes
  • 4 in conversation