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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
mk131190
Obsidian | Level 7

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

jklaverstijn
Rhodochrosite | Level 12

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.

jklaverstijn
Rhodochrosite | Level 12

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26
data want;
  set have;
  new_variable=dhms(your_date,0,0,0);
  format new_variable datetime.;         <--- Modify this format assigning
run;
mk131190
Obsidian | Level 7

That's perfect thanks both.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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