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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 323 views
  • 0 likes
  • 2 in conversation