BookmarkSubscribeRSS Feed
zoomzoom
Obsidian | Level 7


Hello Members,

I need some help on formatting a sas date. I have character field E_Date_Time, where values are in the format "9/1/2011 9:25:36". Now, I need the values in date format as "09/01/2011 09:25:36". My code is not working the way I want it to work. I am trying to extract the date and time from E_Date_Time, format them separately and attach them back. The date and time can be separated by ':' or space.

data want:

     format E_Date mmddyy10.;

set have;

E_Date=input(tranwrd(E_Date_Time, ' 0:00:00', ' '), mmddyy10.);

E_Time=scan(E_Date_Time, -1,' ');

E_Date_Time_New=catx(':', E_Date, E_Time);

run;

The result  of var E_Date_Time_New for the above mentioned value is:

18871:9:25:36

Thank you for your time.

1 REPLY 1
DMoovendhan
Quartz | Level 8

In SAS all the dates will be in numeric format that's the reason you are getting value as 18871 in the code above. Just try the below code you will be getting the proper date.

E_Date=input(E_Date_Time, mmddyy10.);

E_Time=scan(E_Date_Time, -1,' ');

E_Date_Time_New=catx(':',put(E_Date, mmddyy10.), E_Time);

In the third statement I have converted the numric date to character date again, so that you will get the required date format value.

Regards,

Moovendhan D

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 628 views
  • 0 likes
  • 2 in conversation