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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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