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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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