BookmarkSubscribeRSS Feed
mfab
Quartz | Level 8

Hello everybody,

 

perhaps someone knows this gem:

 

When using the datetime18. format, SAS does display the year with the last two digits only.

That is even documented over here: SAS 9.4 Formats and Informats - DATETIMEw.d

 

Now I am wondering why that is. 18 Characters would be enough to display the year with 4 digits and have enough space for all the rest (day, month, time, separators).

So this: "01JAN2015:00:00:00" could be a perfect datetime18. value, but instead it is this: "  01JAN15:00:00:00".

 

Does anyone know the reasons behind this?

 

One thought was that they might save some space for years greater than 9999, when the year would require 5 digits, but then again, regardless of the datetime width, years greater than 9999 are always displayed as '****'. So this should not be the reason.

 

Cheers,

Michael

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Seems quite clear in that link you give.  If its 18. then it is padded with two spaces, this is due to the fact that it leaves room for the seconds optional part.

ballardw
Super User

Note that a datetime past year 9999 does display 2 digit years with datetime18. but not 5 digits

data _null_;
   x = '10MAR15021'd;
   y  = dhms(x,0,0,0);
   put y datetime18. y datetime20. y datetime22. y datetime32.;
run;
mfab
Quartz | Level 8

@RW9: well, leaving space for parts of seconds could be an explanation for datetime18. (although datetime18. could display the full year, whereas datetime18.1 should display only a 2 digit year)

If leaving space for parts of seconds is the explanation, then why does datetime19. display a 4 digit year? This leaves only 1 space. So adding parts of second does not fit, because the separator would fill the one space left, so there is no more space for any value.

datetime19. => 4 digit year

datetime19.1 => 2 digit year

IMHO datetime18. and datetime18.1 should work exactly like that since I would expect somewhat of a consistent behaviour Smiley Frustrated

 

 

@ballardw: yeah that's true. Might be an indication that 2 digit year is sort of hard coded for datetime18. ?

 

ballardw
Super User

Proc format with the Picture date/time directives will let you roll your own datetime format. Here is an example of 18 character with 4 digit year.

 

proc format library=work;
picture mydatetime ( default=18)
low-high = "%d%b%Y:%H:%M:%S"   (datatype=datetime)
;
run;

data _null_;
   x='15Feb2017:10:12:13'dt;
   put x mydatetime. x datetime18.;
run;

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