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

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:

AllanBowe_0-1640872945115.png

 

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?

 

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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.

View solution in original post

7 REPLIES 7
AllanBowe
Barite | Level 11

I think I cracked it:

  PROC FORMAT;
    picture yymmddThhmmss other='%0Y-%0m-%0d %0H:%0M:%0s.0000' (datatype=datetime);
  RUN;
  

Result:

 

AllanBowe_0-1640873933711.png

 

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
sbxkoenk
SAS Super FREQ

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

AllanBowe
Barite | Level 11

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).

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
FreelanceReinh
Jade | Level 19

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.

sbxkoenk
SAS Super FREQ

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

AllanBowe
Barite | Level 11
by jove - that does it!

Thanks, I missed the default option somehow!
/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
Ksharp
Super User

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

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
  • 7 replies
  • 923 views
  • 9 likes
  • 4 in conversation