<?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: Weird output when combining two data sets! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255483#M48792</link>
    <description>&lt;P&gt;OK, but this doesn't explain why the error described in the OP only occurs in the first example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Mar 2016 09:51:21 GMT</pubDate>
    <dc:creator>Norman21</dc:creator>
    <dc:date>2016-03-09T09:51:21Z</dc:date>
    <item>
      <title>Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255434#M48776</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm testing a simple program by merging two datasets in SAS Studio. Here is my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one(drop=i);
do i=1 to 100;
x=log10(i);
output;
end;
run;

data two;
input x y;
cards;
0 3
1 4
2 5
;
run;

data together;
merge one(in=o) two(in=t);
by x;
if o and t;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you run this code, you get the expected output, which is basically the CARDS/DATALINES section of data two. However, if I change the increment of the DO loop in data one from 1 to 0.1, i.e.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one(drop=i);
do i=1 to 100 by 0.1;
x=log10(i);
output;
end;
run;

data two;
input x y;
cards;
0 3
1 4
2 5
;
run;

data together;
merge one(in=o) two(in=t);
by x;
if o and t;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then I get an unexpected/unexplainable output which is only "0 3". Could anyone tell me why I&amp;nbsp;get this strange output and how to fix this issue? Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 04:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255434#M48776</guid>
      <dc:creator>expertyejin</dc:creator>
      <dc:date>2016-03-09T04:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255440#M48779</link>
      <description>&lt;P&gt;This looks like a problem with the precision of the BY variables. LOG10(10) doesn't &lt;EM&gt;exactly&lt;/EM&gt; equal 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might like to try the ROUND function on the log transformed variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 06:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255440#M48779</guid>
      <dc:creator>Norman21</dc:creator>
      <dc:date>2016-03-09T06:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255466#M48784</link>
      <description>&lt;P&gt;To verify your precision problem, just add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;format x best32.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;to your first data step and take a look at observation 91 of dataset one.&lt;/P&gt;
&lt;P&gt;As soon as you have fractions with numbers represented in real format, you get small errors when doing calculations, unless you use fractions that represent easily (and finitely) in binary.&lt;/P&gt;
&lt;P&gt;IOW, adding 0.1 10 times is different from adding 1, as the binary representation of 0.1 (base 10) looks kind of funny, while 1 (base 10) is simply 1 (base 2).&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:11:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255466#M48784</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T09:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255472#M48786</link>
      <description>&lt;P&gt;Even more revealing is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
format x i best32.;
do i=1 to 100 by 0.1;
x=log10(i);
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Just look at the seemingly crazy development of i, especially after obs 747&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:19:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255472#M48786</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T09:19:59Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255477#M48788</link>
      <description>&lt;P&gt;This is very interesting - and worrying!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why doesn't SAS throw up an ERROR (or at least a NOTE) when i is not an integer?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255477#M48788</guid>
      <dc:creator>Norman21</dc:creator>
      <dc:date>2016-03-09T09:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255481#M48790</link>
      <description>&lt;P&gt;Because SAS does not know "integer". A number in SAS is always stored in 8 byte real, and even if you enter a number that is considered an integer, it will be stored as mantissa an exponent. As soon as you need more precision than the mantissa can hold, you get rounding errors.&lt;/P&gt;
&lt;P&gt;Decimal fractions need so many digits in the (binary) mantissa that you very quickly get those rounding errors.&lt;/P&gt;
&lt;P&gt;And why should SAS expect integers when you already introduced fractions by adding&lt;/P&gt;
&lt;PRE&gt;by 0.1;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255481#M48790</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T09:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255482#M48791</link>
      <description>&lt;P&gt;I have another beauty for you.&lt;/P&gt;
&lt;P&gt;Why does this work as expected?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
format x i best32.;
do i=1 to 100 by 0.25;
x=log10(i);
output;
end;
run;

data two;
input x y;
cards;
0 3
1 4
2 5
;
run;

data together;
merge one(in=o) two(in=t);
by x;
if o and t;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:48:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255482#M48791</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T09:48:57Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255483#M48792</link>
      <description>&lt;P&gt;OK, but this doesn't explain why the error described in the OP only occurs in the first example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255483#M48792</guid>
      <dc:creator>Norman21</dc:creator>
      <dc:date>2016-03-09T09:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255485#M48794</link>
      <description>&lt;P&gt;!?!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yet another reason for SAS to provide a NOTE (at least) if the increment is not an "integer".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 09:53:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255485#M48794</guid>
      <dc:creator>Norman21</dc:creator>
      <dc:date>2016-03-09T09:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255487#M48795</link>
      <description>&lt;P&gt;SAS assumes that you know what you are doing when you specifiy a non-integer increment. The "by 0.1" didn't appear there by accident.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am very happy that SAS does not treat me like somebody who left her/his brain at home. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 10:01:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255487#M48795</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T10:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255489#M48796</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18293"&gt;@Norman21&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;OK, but this doesn't explain why the error described in the OP only occurs in the first example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Norman.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Look at this:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/" target="_self"&gt;why-0-point-1-does-not-exist-in-floating-point/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 10:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255489#M48796</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-03-09T10:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255501#M48801</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/57510"&gt;@expertyejin﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The primary issue with your code is the DO loop with step size 0.1. This is one of the "classic" examples of producing numeric representation issues and it is described in&amp;nbsp;&lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#p0ji1unv6thm0dn1gp4t01a1u0g6.htm" target="_blank" rel="nofollow"&gt;Numerical Accuracy in SAS Software&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've written several detailed posts on this topic during the past couple of months. Here is a link to the search results for "numeric representation", restricted to my posts:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/forums/searchpage/tab/message?filter=labels%2CauthorId&amp;amp;q=%22numeric+representation%22&amp;amp;author_id=32733&amp;amp;search_type=thread" target="_blank"&gt;https://communities.sas.com/t5/forums/searchpage/tab/message?filter=labels%2CauthorId&amp;amp;q=%22numeric+representation%22&amp;amp;author_id=32733&amp;amp;search_type=thread&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Among these, &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/decimal-places-Calculaitons/m-p/237781#M43616" target="_blank"&gt;this posting&lt;/A&gt; describes in particular what's "wrong" with 0.1. (I've just seen that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;&amp;nbsp;has posted a link to a similar explanation on an external website.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser﻿&lt;/a&gt;&amp;nbsp;mentioned, not only 0.1, but many decimal fractions introduce numeric representation errors into calculations. Please see the bulleted list in &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/ceil-function-bug/m-p/239414#M44044" target="_blank"&gt;this post&lt;/A&gt; for a quantification of this statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Moreover, for similar reasons it is risky to merge datasets using BY variables whose values are not necessarily integers (such as logarithms).&amp;nbsp;In your example you rely (successfully) on mathematical facts such as log10(100)=2. However, there are many mathematical identities (e.g. 0.1+0.7=0.8 or 1.00E-5=1.000E-5) which are &lt;EM&gt;not&lt;/EM&gt; true in SAS (platform dependent, though) due to numeric representation issues.&amp;nbsp;Further surprising examples can be found in &lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/caluculed-in-sas-proc/m-p/244193#M45480" target="_blank"&gt;this post&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In many cases the ROUND function can be used to cope&amp;nbsp;with these issues (e.g. &lt;FONT face="courier new,courier"&gt;round(0.1+0.7, 1e-9)=0.8&lt;/FONT&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A DO loop should use an integer increment. The decimal fractions can be calculated in the body of the loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one(drop=i);
do i=10 to 1000;
  x=log10(i/10);
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 12:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255501#M48801</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-09T12:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Weird output when combining two data sets!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255930#M48958</link>
      <description>&lt;P&gt;Thank you all for your great help and advice!! And I've considered myself an advanced learner when&amp;nbsp;I'm even stumbled on such a basic concept (sigh...)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2016 20:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weird-output-when-combining-two-data-sets/m-p/255930#M48958</guid>
      <dc:creator>expertyejin</dc:creator>
      <dc:date>2016-03-10T20:44:53Z</dc:date>
    </item>
  </channel>
</rss>

