<?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 Accuracy, format and round in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990600#M380294</link>
    <description>&lt;P&gt;I understand that&amp;nbsp;SAS performs arithmetic in a binary system, but I still have following questions about it:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Variable X1 X2: since variable flag="Y", x1 should be less than x2. However, when variable x1 and x2 are displayed as best32. format,&amp;nbsp; it's "73.75". How could I find the difference directly except using HEXw. format? Is there a way that let X1 display as "73.7499999999999999999999999999999999"?&lt;/LI&gt;&lt;LI&gt;Variable Z1 Z2: actually, x1 should be less than 73.75. It's specified that "&lt;SPAN&gt;When the value to be rounded is approximately halfway between two multiples of the rounding unit, the ROUND function rounds up the absolute value and restores the original sign&lt;/SPAN&gt;" in ROUND function. In this case, it should be 73.7 when I use ROUND function to keep 1 decimal place. However, Z1 is 73.8, why?&lt;/LI&gt;&lt;LI&gt;⁠Variable W1 W2: I want to know what's rule when using PUT function to keep the decimal place? Is it "&lt;SPAN&gt;When the value to be rounded is approximately halfway between two multiples of the rounding unit, the ROUND function rounds up the absolute value and restores the original sign&lt;/SPAN&gt;"?&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	x1=(36.9-31)*12.5;
	x2=73.75;
	if x1 &amp;lt;x2 then flag="Y";

	y1=x1;
	y2=x2;
	format x1 x2 best32. y1 y2 hex16.;

	z1=round(x1,0.1);
	z2=round(x2,0.1);

	w1=strip(put(x1,5.1));
	w2=strip(put(x2,5.1));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jul 2026 05:10:56 GMT</pubDate>
    <dc:creator>AIO</dc:creator>
    <dc:date>2026-07-09T05:10:56Z</dc:date>
    <item>
      <title>Accuracy, format and round</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990600#M380294</link>
      <description>&lt;P&gt;I understand that&amp;nbsp;SAS performs arithmetic in a binary system, but I still have following questions about it:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Variable X1 X2: since variable flag="Y", x1 should be less than x2. However, when variable x1 and x2 are displayed as best32. format,&amp;nbsp; it's "73.75". How could I find the difference directly except using HEXw. format? Is there a way that let X1 display as "73.7499999999999999999999999999999999"?&lt;/LI&gt;&lt;LI&gt;Variable Z1 Z2: actually, x1 should be less than 73.75. It's specified that "&lt;SPAN&gt;When the value to be rounded is approximately halfway between two multiples of the rounding unit, the ROUND function rounds up the absolute value and restores the original sign&lt;/SPAN&gt;" in ROUND function. In this case, it should be 73.7 when I use ROUND function to keep 1 decimal place. However, Z1 is 73.8, why?&lt;/LI&gt;&lt;LI&gt;⁠Variable W1 W2: I want to know what's rule when using PUT function to keep the decimal place? Is it "&lt;SPAN&gt;When the value to be rounded is approximately halfway between two multiples of the rounding unit, the ROUND function rounds up the absolute value and restores the original sign&lt;/SPAN&gt;"?&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	x1=(36.9-31)*12.5;
	x2=73.75;
	if x1 &amp;lt;x2 then flag="Y";

	y1=x1;
	y2=x2;
	format x1 x2 best32. y1 y2 hex16.;

	z1=round(x1,0.1);
	z2=round(x2,0.1);

	w1=strip(put(x1,5.1));
	w2=strip(put(x2,5.1));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2026 05:10:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990600#M380294</guid>
      <dc:creator>AIO</dc:creator>
      <dc:date>2026-07-09T05:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Accuracy, format and round</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990601#M380295</link>
      <description>&lt;P&gt;1)&lt;/P&gt;
&lt;P&gt;For your first question, sas is unable to store 73.75 exactly in computer sometimes, so you can't always expect the expression is equal. If you want to compare them ,the best way is using ROUND() function.&lt;/P&gt;
&lt;P&gt;if you need to display&amp;nbsp;&lt;SPAN&gt;"73.7499999999999999999999999999999999" , you need to resort to PICTURE format.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;STRONG&gt;proc format;
picture fmt
low-high='09.99999999999999'
;
run;&lt;/STRONG&gt;

data test;
	x1=(36.9-31)*12.5;
	x2=73.75;
	if x1 &amp;lt;x2 then flag="Y";
put x1=  &lt;STRONG&gt;fmt32.20&lt;/STRONG&gt; x2= &lt;STRONG&gt;fmt32.20&lt;/STRONG&gt;  flag=;

    call missing(flag);
	if &lt;STRONG&gt;round(x1,0.1)&lt;/STRONG&gt; &amp;lt;&lt;STRONG&gt;round(x2,0.1)&lt;/STRONG&gt; then flag="Y";
put x1=  32.20 x2= 32.20 flag=;



    z1=round(x1,0.1);
	z2=round(x2,0.1);
put z1=  32.20 z2= 32.20;

    w1=strip(put(x1,5.1));
	w2=strip(put(x2,5.1));
put w1= w2=;
run;&lt;/PRE&gt;
&lt;PRE&gt;&lt;STRONG&gt;x1=73.74999999999999&lt;/STRONG&gt; x2=73.75000000000000 flag=Y
x1=73.75000000000000000000 x2=73.75000000000000000000 &lt;STRONG&gt;flag=&lt;/STRONG&gt;
z1=73.80000000000000000000 z2=73.80000000000000000000
w1=73.7 w2=73.8
NOTE: 数据集 WORK.TEST 有 1 个观测和 7 个变量。
NOTE: “DATA 语句”所用时间（总处理时间）:
      实际时间          0.03 秒
      CPU 时间          0.03 秒

&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;2)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For your second question, in sas there is a constant epsolon:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;406  data _null_;
407  ep=constant('MACEPS');
408  put ep=;
409  run;

&lt;STRONG&gt;ep=2.220446E-16
&lt;/STRONG&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Once the difference of two numbers is less than epsolon, then sas would take these two number are same, your situation is this.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3)&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For your third question, I also get surprise at this result. I also think&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;put(x1,5.1)&lt;/LI-CODE&gt;
&lt;P&gt;&lt;SPAN&gt;would be like this&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;put(round(x1,5.1),5.1)&lt;/LI-CODE&gt;
&lt;P&gt;and would expect both are 73.8 . Not one is 73.7 and another is 73.8 .&lt;/P&gt;
&lt;P&gt;I suggest you to contact with &lt;SPAN&gt;&amp;nbsp;SAS -Technical -Support&lt;/SPAN&gt; to discover what is wrong with PUT() .&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2026 07:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990601#M380295</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-07-09T07:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Accuracy, format and round</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990604#M380296</link>
      <description>&lt;P&gt;For Q2, the difference between X1 and X2 is not less than machine epsilon. SAS shouldn't take them as same.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	x1=(36.9-31)*12.5;
	x2=73.75;
	if x1 &amp;lt;x2 then flag="Y";
	diff=x2-x1;
	ep=constant('MACEPS');
	if diff&amp;lt;ep then lsfl="Y";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Jul 2026 10:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990604#M380296</guid>
      <dc:creator>AIO</dc:creator>
      <dc:date>2026-07-09T10:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Accuracy, format and round</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990605#M380297</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426478"&gt;@AIO&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I agree with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;that applying the ROUND function to the result of calculations like that in variable X1 would be best practice as it avoids propagating the unavoidable rounding error (due to numeric representation issues).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While 73.75 does have an exact, finite representation in the binary system (&lt;FONT face="courier new,courier"&gt;1001001.11&lt;/FONT&gt;), your rounding issue comes from the calculation: The result of &lt;FONT face="courier new,courier"&gt;36.9-31&lt;/FONT&gt;, computed in the binary system, using the internal binary floating-point representations of 36.9 (which already contains a numeric representation error) and 31, is slightly less than 5.9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;170   data _null_;
171   d=5.9-(36.9-31);
172   put d;
173   run;

1.776357E-15
&lt;/PRE&gt;
&lt;P&gt;This difference, &lt;FONT face="courier new,courier"&gt;2**-49=1.776356839...E-15&lt;/FONT&gt;, reflects a binary digit, actually &lt;EM&gt;three&lt;/EM&gt; digits 010, which are missing (compared to the internal representation of 5.9) at the end of the mantissa because the internal representation of 36.9 contains &lt;EM&gt;five&lt;/EM&gt; rather than &lt;EM&gt;two&lt;/EM&gt; bits for the integer part at the beginning of the mantissa. Of course, multiplying the result by 12.5 doesn't bring those lost bits back, hence the result &amp;lt;73.75.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To obtain more decimal places than the picture format suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;, you could apply a user-defined function such as &lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/654626/highlight/true#M196566" target="_blank" rel="noopener"&gt;BASECONVFP&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;and translate the internal binary representation of the fractional part of X1 to the decimal system:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;183   data _null_;
184   x1=(36.9-31)*12.5;
185   b=put(x1, binary64.);
186   length dec $50;
187   dec=baseconvfp(substr(b,19),2,10,50);
188   put dec;
189   run;

74999999999998578914528479799628257751464843750000&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;So, adding the integer part, you get&amp;nbsp;&lt;FONT face="courier new,courier"&gt;73.7499999999999857891452847979962825775146484375&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As to your second question: To avoid the "fuzzing" of the ROUND function, use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p1u0w0omad7fyxn1unu83odr6vy2.htm" target="_blank" rel="noopener"&gt;ROUNDZ function&lt;/A&gt;:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;z1=roundz(x1,0.1);
z2=roundz(x2,0.1);&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Result: &lt;FONT face="courier new,courier"&gt;z1=73.7&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;z2=73.8&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Regarding your third question, I think the ROUND function uses a more sophisticated algorithm than the PUT function and hence is the preferred tool for rounding numeric values.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2026 11:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990605#M380297</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-07-09T11:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Accuracy, format and round</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990606#M380298</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426478"&gt;@AIO&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;For Q2, the difference between X1 and X2 is not less than machine epsilon. SAS shouldn't take them as same.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;See the paragraph in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0tj6cmga7p8qln1ejh6ebevm0c9.htm" target="_blank" rel="noopener"&gt;documentation of the ROUND function&lt;/A&gt; starting with "The approximation is relative to the size of the value to be rounded ..." The "fuzzing" of the ROUND function does not use an absolute additive constant (such as machine epsilon).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jul 2026 11:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Accuracy-format-and-round/m-p/990606#M380298</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-07-09T11:36:56Z</dc:date>
    </item>
  </channel>
</rss>

