I'm trying to add decimals (all of them) to my picture format. This is what I have so far (using the new little "s" for milliseconds):
PROC FORMAT;
picture yymmddThhmmss other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime);
RUN;
data _null_;
x=datetime();
put x= yymmddThhmmss.;
put x= yymmddThhmmss.8;
put x= best16.15;
run;
And this is the result:
For some reason, the decimal part is ALWAYS rounded. Does anyone have any ideas for displaying it fully, whilst being accurate both with / without the decimal part in the format?
Hi @AllanBowe,
I think you're using a different SAS version or platform because with my Windows SAS 9.4M5 I get an unwanted additional decimal point with your format:
660 data _null_; 661 x=1956493090.20730710; 662 put x= yymmddThhmmss.; 663 put x= yymmddThhmmss.15; 664 put x= best16.; 665 run; x=2021-12-30 14:18:10.0000 x=2021-12-30 14:18:10.207307.0 x=1956493090.20731
I'd rather specify the length of the format either in the definition or in the PUT statement:
picture yymmddThhmmss (default=28) other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime);
With that (or with other default lengths >=26) I obtain
x=2021-12-30 14:18:10.207307
from the second PUT statement.
I think I cracked it:
PROC FORMAT;
picture yymmddThhmmss other='%0Y-%0m-%0d %0H:%0M:%0s.0000' (datatype=datetime);
RUN;
Result:
Hello Allan,
Glad that you have sorted it out.
(The decimals in your 2nd put statement were cut off by the way, nothing was rounded 😉)
Just this note :
More than 5 decimals for the milliseconds (in a datetime value) is not useful. It's not trustworthy / reliable beyond 5.
You cannot trust the '707' in :
x=2021-12-30 17:20:49.02856707
With the internal 64-bit floating-point representation you get about 15 significant digits.
I remember helping an econom(etri)ics student with her thesis on high-frequency trading on the stock market (I think it was NASDAQ).
Those milli-seconds and calculating with them was an ordeal and a challenge.
Eventually, I did that with a trick, but I don't remember the finer points (the subtleties). But it worked!
But maybe you just want to display instead of doing calculations (?).
Kind regards,
Koen
Hi Koen - thanks for your inputs. That's a great point about 15 significant digits. Perhaps we will indeed need to restrict datetime decimals to 5dp. At this stage, very happy to have >0 dp!
For our use case, all I'm looking to do is send the data from SAS to a web client, unaltered (as much as possible).
Hi @AllanBowe,
I think you're using a different SAS version or platform because with my Windows SAS 9.4M5 I get an unwanted additional decimal point with your format:
660 data _null_; 661 x=1956493090.20730710; 662 put x= yymmddThhmmss.; 663 put x= yymmddThhmmss.15; 664 put x= best16.; 665 run; x=2021-12-30 14:18:10.0000 x=2021-12-30 14:18:10.207307.0 x=1956493090.20731
I'd rather specify the length of the format either in the definition or in the PUT statement:
picture yymmddThhmmss (default=28) other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime);
With that (or with other default lengths >=26) I obtain
x=2021-12-30 14:18:10.207307
from the second PUT statement.
Hello @FreelanceReinh ,
That's a good point.
On my Windows SAS 9.4 M7 @AllanBowe 's last picture format is not working either (same phenomenon .0)
, but on my Full Viya 3.5 LINA upgrade (21w13) (Linux OS) it is working!
I haven't tested VIYA 4 but I am sure it will work on VIYA 4 as well.
Thanks,
Koen
Freelance is right. You need make the length of format bigger .
42 PROC FORMAT; 43 picture yymmddThhmmss other='%0Y-%0m-%0d %0H:%0M:%0s' (datatype=datetime); NOTE: 输出格式 YYMMDDTHHMMSS 已输出。 44 RUN; NOTE: “PROCEDURE FORMAT”所用时间(总处理时间): 实际时间 0.04 秒 CPU 时间 0.01 秒 45 46 data _null_; 47 x=datetime(); 48 put x= yymmddThhmmss.; 49 put x= yymmddThhmmss32.8; 50 put x= best16.15; 51 run; x=2022-01-01 17:49:32 x=2022-01-01 17:49:31.52500009 x=1956678571.525
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.