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

Is it possible to define your own datetime format?  Or is there an existing format that I didn't find? My goal is "yyyy-mm-dd hh:mm:ss", for example, "2018-09-15 14:52:22".  My example below prints a couple close options, but not exactly what I would like.  And I'm actually more interested in writing this format to a csv file, if somehow that permits a different solution than printing.

 

Thanks,

Chris

 

 

 

data a;
mydt = '15sep2018 14:52:22'dt;
proc print data=a; /* 1852642342 */
proc print data=a;
format mydt e8601dt25.0; /* 2018-09-15T14:52:22 */
proc print data=a;
format mydt nldatms.; /* 09/15/2018 14:52:22 */
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Yes very possible:

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


data a;
mydt = '15sep2018 14:52:22'dt;
run;

proc print data=a;
   format mydt customdt.;
run;

You left out some details though. For the time component do you want leading zero:  08:15:30 or 8:15:30?

In the Picture the % starts a directive. Proc format has a bunch of these related to different possible appearance of date, time or datetime options. The characters such a : or blank are treated literally so you can insert which ever separator you might like or none as needed.

Caution: this is one of the few places that you really want to use single quotes around the definition as if you use double quotes then SAS will think you may be attempting to use macros, which are almost certainly not defined, or if they are defined aren't for use in the picture statement.

You have to provide the data type so the range of the underlying numeric value is treated correctly.

View solution in original post

6 REPLIES 6
ballardw
Super User

Yes very possible:

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


data a;
mydt = '15sep2018 14:52:22'dt;
run;

proc print data=a;
   format mydt customdt.;
run;

You left out some details though. For the time component do you want leading zero:  08:15:30 or 8:15:30?

In the Picture the % starts a directive. Proc format has a bunch of these related to different possible appearance of date, time or datetime options. The characters such a : or blank are treated literally so you can insert which ever separator you might like or none as needed.

Caution: this is one of the few places that you really want to use single quotes around the definition as if you use double quotes then SAS will think you may be attempting to use macros, which are almost certainly not defined, or if they are defined aren't for use in the picture statement.

You have to provide the data type so the range of the underlying numeric value is treated correctly.

maguiremq
SAS Super FREQ

You can use the PICTURE statement within PROC FORMAT. There may be a format out there already, but sometimes you have to make your own.

 

data a;
mydt = '15sep2018 14:52:22'dt;
run;

proc format;
	picture customtm 
		other = '%Y-%0m-%0D %0H:%0M:%0S' (datatype = datetime);
run;
Obs mydt 
1 2018-09-15 14:52:22 

Here is a link to the PICTURE statement documentation. You can look around underneath the round header and see all the custom formatting you can do with dates and times.

 

ChrisAndrews
Fluorite | Level 6
Thanks for the code and link. I added (round default=19) after reading more about the options.
ChrisAndrews
Fluorite | Level 6
Thanks for the helpful link.
ChrisAndrews
Fluorite | Level 6
Thanks.  I tried to imply I wanted both digits by writing hh:mm:dd.  Regardless, your answer works for me.

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
  • 6 replies
  • 1257 views
  • 5 likes
  • 4 in conversation