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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 531 views
  • 2 likes
  • 4 in conversation