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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 3 replies
  • 161 views
  • 2 likes
  • 4 in conversation