<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to subtract by fraction of a second for two separate times? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784918#M250472</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/400328"&gt;@theflunkee&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would apply the ROUND function to the difference so as to obtain a clean result:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;TimeBetter=&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;round(&lt;/STRONG&gt;&lt;/FONT&gt;time_mlead1-time_m&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;, 1e-9)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Small differences between relatively large numbers are particularly prone to (relative) rounding errors due to numeric representation issues. Let's take the second record of your sample data as an example, using Windows SAS 9.4M5:&lt;/P&gt;
&lt;PRE&gt;301  data _null_;
302  time_mlead1='09:30:00.002551't;
303  time_m='09:30:00.002524't;
304  TimeBetter=round(time_mlead1-time_m, 1e-9);
305  TimeBroken=time_mlead1-time_m;
306  put (TimeB:)(=best16. /);
307  run;

TimeBetter=0.000027
TimeBroken=0.00002699999&lt;FONT color="#FF0000"&gt;459&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Even with the standard format BEST16. the rounding error in &lt;FONT face="courier new,courier"&gt;TimeBroken&lt;/FONT&gt; becomes obvious and of course the condition&amp;nbsp;&lt;FONT face="courier new,courier"&gt;TimeBroken=0.000027&lt;/FONT&gt; does &lt;EM&gt;not&lt;/EM&gt; hold, whereas &lt;FONT face="courier new,courier"&gt;TimeBetter=0.000027&lt;/FONT&gt;&amp;nbsp;is true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The relative error in &lt;FONT face="courier new,courier"&gt;TimeBroken&lt;/FONT&gt; is approx. &lt;FONT face="courier new,courier"&gt;2.0E-5&lt;/FONT&gt; percent, which is more than &lt;EM&gt;five billion times larger&lt;/EM&gt; than the unavoidable relative numeric representation error in&amp;nbsp;&lt;FONT face="courier new,courier"&gt;TimeBetter&lt;/FONT&gt; (using the ROUND function) of approx. &lt;FONT face="courier new,courier"&gt;3.7E-15&lt;/FONT&gt;&amp;nbsp;percent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of your 20 example records, except the two trivial cases with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;time_mlead1=time_m&lt;/FONT&gt;, would produce a perfect result without cleaning the rounding error.&lt;/P&gt;</description>
    <pubDate>Wed, 08 Dec 2021 14:21:07 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-12-08T14:21:07Z</dc:date>
    <item>
      <title>How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784870#M250460</link>
      <description>&lt;P&gt;&lt;STRONG&gt;I have code like the following:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data a_cal; set Work.a_bgn;
Format TimeBetter Time17.9 ;
TimeBetter=time_mlead1-time_m;
where time_m&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="theflunkee_0-1638960256129.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66501i67E3CD8DA99790E6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="theflunkee_0-1638960256129.png" alt="theflunkee_0-1638960256129.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I want to calculate the diff between time_mlead1 and time_m in fractions of a second.&lt;BR /&gt;e.g. for row 1: the answer should be 0.00087 seconds, and I don't want it to be rounded to 0 seconds or 1 second.&lt;/P&gt;&lt;P&gt;INTCK seems only work for integer seconds only.&amp;nbsp;&lt;BR /&gt;It is ok to be in any format (date or number), but I want to calculate sum of the difference later.&lt;BR /&gt;How should I rewrite my code? Many Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 11:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784870#M250460</guid>
      <dc:creator>theflunkee</dc:creator>
      <dc:date>2021-12-08T11:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784875#M250462</link>
      <description>&lt;P&gt;Just do a data step subtraction. Which you appear to have done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What problems are you having? Doesn't your code work? What is wrong with it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 11:35:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784875#M250462</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-08T11:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784876#M250463</link>
      <description>&lt;P&gt;the above code would yield me 0:00:00.000000000.&lt;BR /&gt;&lt;BR /&gt;I have also converted the time_m and time_mlead1's format to be time17.9....don't know why it wouldn't work&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 11:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784876#M250463</guid>
      <dc:creator>theflunkee</dc:creator>
      <dc:date>2021-12-08T11:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784885#M250465</link>
      <description>&lt;P&gt;I am not having this problem, so I cannot say why you are having this problem. Formats are irrelevant here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    time_m='9:30:00.002437't;
    time_m_lead1='9:30:00.002524't;
    difference=time_m_lead1-time_m;
    format time_m time_m_lead1 difference time17.9;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG"&gt;&lt;img src="https://communities.sas.com/skins/images/710AFAD0E2288E4865F0ACA859F48344/responsive_peak/images/image_not_found.png" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 11:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784885#M250465</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-08T11:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784887#M250466</link>
      <description>&lt;P&gt;Your code is OK, just use a proper format for the result, like 10.6&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 12:02:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784887#M250466</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-12-08T12:02:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to subtract by fraction of a second for two separate times?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784918#M250472</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/400328"&gt;@theflunkee&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would apply the ROUND function to the difference so as to obtain a clean result:&lt;/P&gt;
&lt;PRE&gt;&lt;FONT size="4"&gt;TimeBetter=&lt;FONT color="#3366FF"&gt;&lt;STRONG&gt;round(&lt;/STRONG&gt;&lt;/FONT&gt;time_mlead1-time_m&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;, 1e-9)&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Small differences between relatively large numbers are particularly prone to (relative) rounding errors due to numeric representation issues. Let's take the second record of your sample data as an example, using Windows SAS 9.4M5:&lt;/P&gt;
&lt;PRE&gt;301  data _null_;
302  time_mlead1='09:30:00.002551't;
303  time_m='09:30:00.002524't;
304  TimeBetter=round(time_mlead1-time_m, 1e-9);
305  TimeBroken=time_mlead1-time_m;
306  put (TimeB:)(=best16. /);
307  run;

TimeBetter=0.000027
TimeBroken=0.00002699999&lt;FONT color="#FF0000"&gt;459&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;Even with the standard format BEST16. the rounding error in &lt;FONT face="courier new,courier"&gt;TimeBroken&lt;/FONT&gt; becomes obvious and of course the condition&amp;nbsp;&lt;FONT face="courier new,courier"&gt;TimeBroken=0.000027&lt;/FONT&gt; does &lt;EM&gt;not&lt;/EM&gt; hold, whereas &lt;FONT face="courier new,courier"&gt;TimeBetter=0.000027&lt;/FONT&gt;&amp;nbsp;is true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The relative error in &lt;FONT face="courier new,courier"&gt;TimeBroken&lt;/FONT&gt; is approx. &lt;FONT face="courier new,courier"&gt;2.0E-5&lt;/FONT&gt; percent, which is more than &lt;EM&gt;five billion times larger&lt;/EM&gt; than the unavoidable relative numeric representation error in&amp;nbsp;&lt;FONT face="courier new,courier"&gt;TimeBetter&lt;/FONT&gt; (using the ROUND function) of approx. &lt;FONT face="courier new,courier"&gt;3.7E-15&lt;/FONT&gt;&amp;nbsp;percent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;None of your 20 example records, except the two trivial cases with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;time_mlead1=time_m&lt;/FONT&gt;, would produce a perfect result without cleaning the rounding error.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Dec 2021 14:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-subtract-by-fraction-of-a-second-for-two-separate-times/m-p/784918#M250472</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-12-08T14:21:07Z</dc:date>
    </item>
  </channel>
</rss>

