<?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: Interesting result when where condition is applied for dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896675#M354321</link>
    <description>&lt;P&gt;Computers can have trouble getting exact numbers.&amp;nbsp; It's just a function of the storage system for numeric values.&amp;nbsp; Try it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where round(theta, 0.000001) = 1.6;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Some detail on the issue:&amp;nbsp; &lt;A href="https://documentation.sas.com/doc/en/lrcon/9.4/p0ji1unv6thm0dn1gp4t01a1u0g6.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/lrcon/9.4/p0ji1unv6thm0dn1gp4t01a1u0g6.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 02 Oct 2023 14:11:15 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-10-02T14:11:15Z</dc:date>
    <item>
      <title>Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896665#M354320</link>
      <description>&lt;P&gt;Hello community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please help me understand the puzzle/mystery here. I have created a dataset (chk) using the below code. When I do apply a subset condition for the dataset, it results in 0 records when theta value is &amp;gt;= 1.6. The same 0 records is the outcome when a where condition is applied to the dataset manually in sas window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data chk;&lt;BR /&gt;do miu=2 to 3.5 by 0.5;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;do sd=0.7 to 1.3 by 0.3;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;do theta=0.8 to 2.8 by 0.2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; do n=6 to 30 by 4;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data chk1;&lt;BR /&gt;set chk;&lt;BR /&gt;where theta = 1.6;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank You.&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 13:30:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896665#M354320</guid>
      <dc:creator>thummala</dc:creator>
      <dc:date>2023-10-02T13:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896675#M354321</link>
      <description>&lt;P&gt;Computers can have trouble getting exact numbers.&amp;nbsp; It's just a function of the storage system for numeric values.&amp;nbsp; Try it this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where round(theta, 0.000001) = 1.6;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Some detail on the issue:&amp;nbsp; &lt;A href="https://documentation.sas.com/doc/en/lrcon/9.4/p0ji1unv6thm0dn1gp4t01a1u0g6.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/lrcon/9.4/p0ji1unv6thm0dn1gp4t01a1u0g6.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 14:11:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896675#M354321</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-10-02T14:11:15Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896686#M354325</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;I faced the same situation in the past and what I did was the following:&lt;/P&gt;
&lt;P&gt;1. specified the length of the fields&lt;/P&gt;
&lt;P&gt;2. also the format of the fields&lt;/P&gt;
&lt;P&gt;3. the character representation of the numeric and then filtered it out&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data chk;
	length miu 8;
	length sd 8;
	length theta 8;
	length n 8;

	do miu=2 to 3.5 by 0.5;
		do sd=0.7 to 1.3 by 0.3;
			do theta=0.8 to 2.8 by 0.2;
				do n=6 to 30 by 4;
					output;
				end;
			end;
		end;
	end;

	format miu 8.;
	format sd 18.1;
	format theta 18.1;
	format n 8.;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then for the filtering part&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data chk1;
	set chk;
	theta_c = input(theta, $20.);
run;

data chk2;
set chk1;
where theta_c ="1.6";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 15:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896686#M354325</guid>
      <dc:creator>vfarmak</dc:creator>
      <dc:date>2023-10-02T15:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896691#M354326</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/40853"&gt;@thummala&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively, use an integer index variable and then compute the non-integer values as needed. This avoids the tiny rounding errors (which cause the sort of problem you have encountered) right from the start.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;do _t=8 to 28 by 2;
  theta=_t/10;
...
drop _:;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In a loop over very few values (such as your &lt;FONT face="courier new,courier"&gt;sd&lt;/FONT&gt; loop, but that happened to produce "clean" values) you can list them like this:&lt;/P&gt;
&lt;PRE&gt;do sd=0.7, 1, 1.3;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 15:08:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896691#M354326</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-10-02T15:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896697#M354330</link>
      <description>&lt;P&gt;Computer is just a very fast abacus, it adds, multiplies, and subtracts integers fairly good, but floating point numbers is totally different suit.&lt;/P&gt;
&lt;P&gt;And it is not only SAS case, in R Python, Excel, C, Java, etc. you will be able to reproduce example similar to below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would expect to get 1 observation form:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  do i = -1 to 1 by 0.1;
    if 0=i then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but you get 0, as the log states:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    data test;
2      do i = -1 to 1 by 0.1;
3        if 0=i then output;
4      end;
5    run;

NOTE: The data set WORK.TEST has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case it's fault of infinite extension of one-tenth (1/10) in binary system, similar way one-third (1/3) has 0.333333... in the decimal system.&lt;/P&gt;
&lt;P&gt;Lecture of the link from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&amp;nbsp;will help you to understand the situation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 15:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896697#M354330</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-10-02T15:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Interesting result when where condition is applied for dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896779#M354355</link>
      <description>&lt;P&gt;You already had some good suggestions on how to solve this - the problem you are encountering is the following:&lt;/P&gt;
&lt;P&gt;Numbers in SAS are internally represented as base 2 floating points (unless you are on the mainframe, then they are base 16 floating points). This means that some decimal numbers can be represented exactly, for instance your MIU values:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do miu=2 to 3.5 by 0.5;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;- here you will have 2E0, 5E-1, 3E0 etc. up to 7E-1, as the numbers are all possible to calculate as integers, some of them divided by 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your thetas are not like that - the number 0.2 cannot be represented exactly as an integer divided by a power of 2. So when you keep adding 0.2 to a decimal number, the result will eventually become inexact. In the case you show, this happens at 1.6.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One other possibility, apart from rounding or using integers instead of decimals is to spell out the numbers you want in a list:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do theta=0.8,1.0,1.2...2.8;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will give the "exact" decimal numbers. One way to do it is like this - put the list of values you want in a macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length thetas $200;
  do theta=0.8 to 2.8 by 0.2;
    call catx(',',thetas,put(theta,3.1));
    end;
  call symputx('thetas',thetas);
run;
  
data chk;
do miu=2 to 3.5 by 0.5;
         do sd=0.7 to 1.3 by 0.3;
                   do theta=&amp;amp;thetas;
                              do n=6 to 30 by 4;
                              output;
                              end;
                   end;
           end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;- of course, you may want to do the same for the SD values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  length thetas $200;
  do theta=0.8 to 2.8 by 0.2;
    call catx(',',thetas,put(theta,3.1));
    end;
  call symputx('thetas',thetas);
  length sds $200;
    do sd=0.7 to 1.3 by 0.3;
    call catx(',',sds,put(sd,3.1));
    end;
  call symputx('sds',sds);
run;
  
data chk;
do miu=2 to 3.5 by 0.5;
         do sd=&amp;amp;sds;
                   do theta=&amp;amp;thetas;
                              do n=6 to 30 by 4;
                              output;
                              end;
                   end;
           end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this worth the trouble? Maybe, if you are doing a lot of comparisons on the same data - then it probably will be faster than rounding every time you try to look up a value. And if you have a lot of inexperienced users trying to find the different values, this solution may save a lot of time - not just CPU time, but actual working hours, which are a lot more expensive.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 02 Oct 2023 19:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Interesting-result-when-where-condition-is-applied-for-dataset/m-p/896779#M354355</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-10-02T19:25:25Z</dc:date>
    </item>
  </channel>
</rss>

