Date to Datetime

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Date to Datetime

I need to turn all of my date variables to datetime in the format YYYY-MM-DD HH:MMSmiley FrustratedS is there a quick way to do this?


Accepted Solutions
Solution
‎03-31-2016 06:05 AM
Super User
Super User
Posts: 7,942

Re: Date to Datetime

data want;
  set have;
  new_variable=dhms(your_date,0,0,0);
  format new_variable datetime.;         <--- Modify this format assigning
run;

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: Date to Datetime

You would need to specify the missing components, for example:

data want;
  set have;
  new_variable=dhms(your_date,0,0,0);
  format new_variable datetime.;
run;
Occasional Contributor
Posts: 19

Re: Date to Datetime

Are there any other datetime options? This gives DDMMMYY:HH:MMSmiley FrustratedS which isnt the one I needed.

Super Contributor
Posts: 436

Re: Date to Datetime

There are plenty: Formats by Category

 

And if none fit your bill you can roll your own. Look at PROC FORMAT, specifically te PICTURE statement with the DATATYPE=DATETIME directive. It knows no bounds.

 

Hope this helps,

- Jan.

Super Contributor
Posts: 436

Re: Date to Datetime

Posted in reply to jklaverstijn

And I almost forgot: a variable is not in a specific format. It can be a numeric variable with the presentation lossely coupled. You can present your data in any format without actually modyfying the values in the table. data and presentation are separate unless you choose to store the dates as character.

 

- Jan.

Solution
‎03-31-2016 06:05 AM
Super User
Super User
Posts: 7,942

Re: Date to Datetime

data want;
  set have;
  new_variable=dhms(your_date,0,0,0);
  format new_variable datetime.;         <--- Modify this format assigning
run;
Occasional Contributor
Posts: 19

Re: Date to Datetime

That's perfect thanks both.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 395 views
  • 4 likes
  • 3 in conversation