BookmarkSubscribeRSS Feed
shlomiohana
Obsidian | Level 7

Hello,
I have a table called POLAIM_MERGE that includes the field LOAD_LAST_DATETIME (format=datetime20, informat=datetime20)

shlomiohana_1-1683565645171.png

data POLAIM_ADD_FORMAT;
     set POLAIM_MERGE;
     mydate =datepart(LOAD_LAST_DATETIME);
     mytime = timepart(LOAD_LAST_DATETIME);
     format mydate ddmmyy10. mytime time5.;
     LOAD_LAST_DATETIME_NEW= put(mydate,ddmmyy10.)||' '||put(mytime,time5.);
run;

I wrote the following code but I get in the field "mytime" the time 9:03, I would like the leading 0 to be displayed at the time, desired display: 09:03 How do I change the code to show as I need?

shlomiohana_2-1683565767645.png

 

Thanks.

3 REPLIES 3
Kathryn_SAS
SAS Employee

The TODw. format will write a leading 0.

data POLAIM_ADD_FORMAT;
     set POLAIM_MERGE;
     mydate =datepart(LOAD_LAST_DATETIME);
     mytime = timepart(LOAD_LAST_DATETIME);
     format mydate ddmmyy10. mytime tod5.;
     LOAD_LAST_DATETIME_NEW= put(mydate,ddmmyy10.)||' '||put(mytime,tod5.);
run;
Reeza
Super User
Why the character variable conversion? Would a custom format that remained datetime be useful as it would then sort correctly?
ballardw
Super User

Why are you bothering to create a character value for that value? It won't sort properly and can't be used an form of calculation.

 

You can create a custom format to display the existing value with a different appearance:

proc format;
picture mydatetime
low-high ='%0d/%0m/%Y %0H:%0M' (datatype=datetime);
run;

data example;
   x='07May2023:09:12:01'dt;
   format x mydatetime.;
run;

This is one place that you really want to use single quotes to prevent confusion with the macro processor and the format directives. The CASE of the letters in the directives for the date and time portions are critical.

The / space and : are inserted in the relative positions.

 

 

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

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
  • 3 replies
  • 220 views
  • 2 likes
  • 4 in conversation