DATA Step, Macro, Functions and more

Number of digits for year when using datetime18. format

Reply
Frequent Contributor
Posts: 114

Number of digits for year when using datetime18. format

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

Super User
Super User
Posts: 7,401

Re: Number of digits for year when using datetime18. format

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.

Super User
Posts: 10,500

Re: Number of digits for year when using datetime18. format

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;
Frequent Contributor
Posts: 114

Re: Number of digits for year when using datetime18. format

@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. ?

 

Super User
Posts: 10,500

Re: Number of digits for year when using datetime18. format

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;
Ask a Question
Discussion stats
  • 4 replies
  • 127 views
  • 0 likes
  • 3 in conversation