<?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: Mysterious In Function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431767#M106847</link>
    <description>&lt;P&gt;In addition to the good practice info from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;(about rounding) and DECIMAL precision tidbits by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;, I'll offer this.&amp;nbsp; The IN function is one of those lightly documented functions that many users try to squeeze more uses from than maybe they should.&amp;nbsp; It's a natural temptation for those familiar with the IN clause in SQL -- but that works differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, you might be able to get what you want, precision aside, by comparing the formatted values you seek.&amp;nbsp; Here's your original code adjusted to use of &lt;A href="http://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0zs0pv38mel2jn1in4lte2akx4d.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;the WHICHN function (which is documented thoroughly)&lt;/A&gt;.&amp;nbsp; Note that I added commas to your delimited list of&amp;nbsp;&lt;STRONG&gt;nodes&lt;/STRONG&gt; to generate the proper syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nodes=-0.1, 0, 0.1, 0.2;

%let x_min=-0.2;
%let x_max=0.2;
%let x_step=0.01;

data xxx(keep=x nodes_flag);
    nstep=round((&amp;amp;x_max-&amp;amp;x_min)/&amp;amp;x_step);
	do iter=0 to nstep;
	nodes_flag=0;
	x=&amp;amp;x_min+&amp;amp;x_step*iter;
	if whichn(put(x,best12.), &amp;amp;nodes) then nodes_flag=1;
	output;
	end;
run;

data nodes;
  set xxx;
  if nodes_flag=1;
  run;
  proc print data=nodes;
  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 29 Jan 2018 13:05:15 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2018-01-29T13:05:15Z</dc:date>
    <item>
      <title>Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431578#M106776</link>
      <description>&lt;P&gt;I tried using in function to find observation values matching any value of a given series; however, SAS does not always give me the correct answer and the mistakes seem to happen when the give numbers are in decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a piece of sample code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nodes=-0.1 0 0.1 0.2;

%let x_min=-0.2;
%let x_max=0.2;
%let x_step=0.01;

data xxx(keep=x nodes_flag);
    nstep=round((&amp;amp;x_max-&amp;amp;x_min)/&amp;amp;x_step);
	do iter=0 to nstep;
	nodes_flag=0;
	x=&amp;amp;x_min+&amp;amp;x_step*iter;
	if x in (&amp;amp;nodes) then nodes_flag=1;
	output;
	end;
run;

data nodes;
  set xxx;
  if nodes_flag=1;
  run;
  proc print data=nodes;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The dataset "nodes" is suppose to capture the x values if it is any of (-0.1 0 0.1 0.2); however, below is what SAS gives me:&lt;/P&gt;
&lt;P&gt;nodes_flag x&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;-0.1&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;0&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;0.2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It missed 0.1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I change the set values to -0.1 0 0.1, SAS gives me:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;-0.1&lt;BR /&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It still missed 0.1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I change the set values to be 0.1 0.05 0, SAS can only select 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can some one explain how the function works and what I did wrong?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 05:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431578#M106776</guid>
      <dc:creator>bigbigben</dc:creator>
      <dc:date>2018-01-28T05:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431580#M106777</link>
      <description>&lt;P&gt;You're doing math with fractions, which can (and will) cause artifacts because of the limitations of the 8-byte real format SAS uses for numbers. Apply the round() function before comparing.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 07:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431580#M106777</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-28T07:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431605#M106787</link>
      <description>&lt;P&gt;The safest way to eliminate the issue is to deal with integers the whole time.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let x = -10 0 10 20;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let x_min = -20;&lt;/P&gt;
&lt;P&gt;%let x_max = 20;&lt;/P&gt;
&lt;P&gt;%let x_step = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then just before the OUTPUT statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x = x / 100;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 13:26:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431605#M106787</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-01-28T13:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431616#M106793</link>
      <description>&lt;P&gt;SAS numeric values are all double-precision floating point data types, which guarantee only an approximate representation of decimal data. The issues with representing decimal values in floating point are common to all computing platforms.&amp;nbsp; For a deep dive, check &lt;A href="http://documentation.sas.com/?docsetId=lrcon&amp;amp;docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;Numerical Accuracy in SAS Software&lt;/A&gt;. So, to resolve your problem keeping the values as-is, you need a different way of representing numeric values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fortunately for you, Base SAS comes with the DS2 language, which is capable of handling DECIMAL fixed-point numerics values. Decimal values provide exact representation of the decimal values.&amp;nbsp; This DS2 code will provide the results you seek:&lt;/P&gt;
&lt;PRE lang="SAS"&gt;
%let nodes=-0.1 0 0.1 0.2;
%let x_min=-0.2;
%let x_max=0.2;
%let x_step=0.01;

proc ds2;
data xxx/overwrite=yes;
   dcl decimal(5,2) x;
   dcl int nodes_flag; 
   method run();
      dcl int nstep iter;
      nstep=round((&amp;amp;x_max-&amp;amp;x_min)/&amp;amp;x_step);
   	do iter=0 to nstep;
            nodes_flag=0;
            x=&amp;amp;x_min+&amp;amp;x_step*iter;
            if x in (&amp;amp;nodes) then nodes_flag=1;
            output;
   	end;
   end;
enddata;
run;
quit;

data nodes;
   set xxx;
   if nodes_flag=1;
run;
proc print data=nodes;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2018 16:06:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431616#M106793</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2018-01-28T16:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431667#M106820</link>
      <description>&lt;P&gt;Keep everything well rounded with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;x=round(&amp;amp;x_min+&amp;amp;x_step*iter, &amp;amp;x_step);&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 04:42:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431667#M106820</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-29T04:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431692#M106827</link>
      <description>&lt;P&gt;Here a slightly optimized and visually structured version of your code, and expanded with the round() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nodes=-0.1 0 0.1 0.2;

%let x_min=-0.2;
%let x_max=0.2;
%let x_step=0.01;

data nodes (
  keep=x nodes_flag
  where=(nodes_flag = 1)
);
  nstep = round((&amp;amp;x_max. - &amp;amp;x_min.) / &amp;amp;x_step.);
	do iter = 0 to nstep;
	  nodes_flag = 0;
	  x = round(&amp;amp;x_min. + &amp;amp;x_step. * iter, .01);
	  if x in (&amp;amp;nodes.) then nodes_flag = 1;
	  output;
	end;
run;

proc print data=nodes noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;nodes_
 flag        x

   1      -0.1
   1       0.0
   1       0.1
   1       0.2
&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2018 07:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431692#M106827</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-29T07:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431767#M106847</link>
      <description>&lt;P&gt;In addition to the good practice info from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;(about rounding) and DECIMAL precision tidbits by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;, I'll offer this.&amp;nbsp; The IN function is one of those lightly documented functions that many users try to squeeze more uses from than maybe they should.&amp;nbsp; It's a natural temptation for those familiar with the IN clause in SQL -- but that works differently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, you might be able to get what you want, precision aside, by comparing the formatted values you seek.&amp;nbsp; Here's your original code adjusted to use of &lt;A href="http://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p0zs0pv38mel2jn1in4lte2akx4d.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;the WHICHN function (which is documented thoroughly)&lt;/A&gt;.&amp;nbsp; Note that I added commas to your delimited list of&amp;nbsp;&lt;STRONG&gt;nodes&lt;/STRONG&gt; to generate the proper syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let nodes=-0.1, 0, 0.1, 0.2;

%let x_min=-0.2;
%let x_max=0.2;
%let x_step=0.01;

data xxx(keep=x nodes_flag);
    nstep=round((&amp;amp;x_max-&amp;amp;x_min)/&amp;amp;x_step);
	do iter=0 to nstep;
	nodes_flag=0;
	x=&amp;amp;x_min+&amp;amp;x_step*iter;
	if whichn(put(x,best12.), &amp;amp;nodes) then nodes_flag=1;
	output;
	end;
run;

data nodes;
  set xxx;
  if nodes_flag=1;
  run;
  proc print data=nodes;
  run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2018 13:05:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/431767#M106847</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2018-01-29T13:05:15Z</dc:date>
    </item>
    <item>
      <title>Re: Mysterious In Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/434198#M107735</link>
      <description>&lt;P&gt;Thank you all. All these solutions taught me something about SAS. I really appreciate all the help.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:28:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mysterious-In-Function/m-p/434198#M107735</guid>
      <dc:creator>bigbigben</dc:creator>
      <dc:date>2018-02-05T15:28:58Z</dc:date>
    </item>
  </channel>
</rss>

