BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sajid01
Meteorite | Level 14

Hello
I want to convert dates from the  the format "17Aug2020: 10:11:12" to "17 August 2020 10:11:12".
Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you mean as in exporting to Excel or a format in SAS to display the values with that appearance?

Do you want leading zeroes for days 1 to 9 of a month? Do you want a 24-hour clock (hour = 0 to 23) or 12-hour (hour=0 to 11)? Should the hours/minutes/ seconds display leading zeroes   09:04:01 or 9:4:1?

 

Proc Format will let you create a wide variety of displays for date, time and datetime values.

 

Example:

proc format;
picture customdt (default=26)
low-high= '%d %B %Y %0H:%0M:%0S' (datatype=datetime)
;
run;


Data example;
  x='17AUG2020:10:11:12'dt;
  put x customdt.;
run;

Look in the log to see the result of the Put.

The format code is very dependent upon case as the different codes can mean different things when capitalized. I placed 0 (zeroes) in the time portion to keep it more consistent. The spaces and the : in the directives for the format are literal text. Remove them and there will not be spaces or : between elements. The Proc format directives for a format display is one of the few places that you must use single quotes instead of choosing between single and double. If you use double quotes you will get a lot of messages about undefined macros because the % is the indication of the start of a macro call.

A default display length is set to insure that the seconds display. The varying length of text for month names means that the system defaults for length might not display all the values with long month names like September. The month of May will use less space.

 

Read the documentation for the Format Procedure Picture statement for more details.

 

 

Since the value of the variable is numeric the default display in most SAS procedures will right justify the result.

View solution in original post

3 REPLIES 3
ballardw
Super User

Do you mean as in exporting to Excel or a format in SAS to display the values with that appearance?

Do you want leading zeroes for days 1 to 9 of a month? Do you want a 24-hour clock (hour = 0 to 23) or 12-hour (hour=0 to 11)? Should the hours/minutes/ seconds display leading zeroes   09:04:01 or 9:4:1?

 

Proc Format will let you create a wide variety of displays for date, time and datetime values.

 

Example:

proc format;
picture customdt (default=26)
low-high= '%d %B %Y %0H:%0M:%0S' (datatype=datetime)
;
run;


Data example;
  x='17AUG2020:10:11:12'dt;
  put x customdt.;
run;

Look in the log to see the result of the Put.

The format code is very dependent upon case as the different codes can mean different things when capitalized. I placed 0 (zeroes) in the time portion to keep it more consistent. The spaces and the : in the directives for the format are literal text. Remove them and there will not be spaces or : between elements. The Proc format directives for a format display is one of the few places that you must use single quotes instead of choosing between single and double. If you use double quotes you will get a lot of messages about undefined macros because the % is the indication of the start of a macro call.

A default display length is set to insure that the seconds display. The varying length of text for month names means that the system defaults for length might not display all the values with long month names like September. The month of May will use less space.

 

Read the documentation for the Format Procedure Picture statement for more details.

 

 

Since the value of the variable is numeric the default display in most SAS procedures will right justify the result.

ballardw
Super User

One thing to remember about custom formats: it is your responsibility to make them available when needed as they are not supplied by SAS. So either you will need to run the code in each session or create a permanent library for the format(s) and tell SAS where to look for them, system option FMTSEARCH.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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