<?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 How to check variables for equality, within tolerance, niether missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234678#M308511</link>
    <description>&lt;P&gt;I want to compare two variables and return 'true' if they are equal within a specified tolerance, otherwsie 'false', and return 'false' if one of them is missing but 'true' if they are both missing.&amp;nbsp; Is there an easy way to do this in a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;y&lt;/TD&gt;&lt;TD&gt;f(x,y)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.5000000&lt;/TD&gt;&lt;TD&gt;0.5000001&lt;/TD&gt;&lt;TD&gt;TRUE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.5000000&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;FALSE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;TRUE&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Fri, 13 Nov 2015 18:38:28 GMT</pubDate>
    <dc:creator>David_Whitaker</dc:creator>
    <dc:date>2015-11-13T18:38:28Z</dc:date>
    <item>
      <title>How to check variables for equality, within tolerance, niether missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234678#M308511</link>
      <description>&lt;P&gt;I want to compare two variables and return 'true' if they are equal within a specified tolerance, otherwsie 'false', and return 'false' if one of them is missing but 'true' if they are both missing.&amp;nbsp; Is there an easy way to do this in a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;x&lt;/TD&gt;&lt;TD&gt;y&lt;/TD&gt;&lt;TD&gt;f(x,y)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.5000000&lt;/TD&gt;&lt;TD&gt;0.5000001&lt;/TD&gt;&lt;TD&gt;TRUE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.5000000&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;FALSE&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;TRUE&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 13 Nov 2015 18:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234678#M308511</guid>
      <dc:creator>David_Whitaker</dc:creator>
      <dc:date>2015-11-13T18:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to check variables for equality, within tolerance, niether missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234679#M308512</link>
      <description>&lt;P&gt;Use Round() :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data test;
input x y;
format x y best12.;
datalines;
0.5000000	0.5000001
0.5000000	.
.	.	
;

%let tolerance=1e-6;

data want;
set test;
f = round(x,&amp;amp;tolerance.) = round(y,&amp;amp;tolerance.);
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 18:54:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234679#M308512</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-13T18:54:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to check variables for equality, within tolerance, niether missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234680#M308513</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover;
input x	y;	
format x 10.7 y 10.7;
comp_flag=ifc(round(x,0.000001)=round(y,0.000001),'Ture','Fail');
cards;
0.5000000	0.5000001	
0.5000000     .
.             .
;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Nov 2015 18:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234680#M308513</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2015-11-13T18:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to check variables for equality, within tolerance, niether missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234689#M308514</link>
      <description>&lt;P&gt;I would advise you to stay away from the ROUND function on each of the values. &amp;nbsp;It will not always give you the result you want. &amp;nbsp;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;tolerance = 0.01;&lt;/P&gt;
&lt;P&gt;x = 2.004;&lt;/P&gt;
&lt;P&gt;y = 1.993;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you round each variable separately, you'll get 2 - 1.99 which is TRUE. &amp;nbsp;But the difference between them is 0.011 which is FALSE. &amp;nbsp;Instead, take the absolute value of the difference and compare that to your tolerance level. &amp;nbsp;It's a little clumsy, but you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length result $ 5;&lt;/P&gt;
&lt;P&gt;if x = y = . then result = 'TRUE';&lt;/P&gt;
&lt;P&gt;else if x=. then result = 'FALSE';&lt;/P&gt;
&lt;P&gt;else if y=. then result = 'FALSE';&lt;/P&gt;
&lt;P&gt;else do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if abs(x - y) &amp;lt;= tolerance then result='TRUE';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else result='FALSE';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There may be slicker ways to code this, but stay away from ROUND.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 19:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234689#M308514</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-11-13T19:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to check variables for equality, within tolerance, niether missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234710#M308515</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I would advise you to stay away from the ROUND function on each of the values. &amp;nbsp;It will not always give you the result you want. &amp;nbsp;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;tolerance = 0.01;&lt;/P&gt;
&lt;P&gt;x = 2.004;&lt;/P&gt;
&lt;P&gt;y = 1.993;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you round each variable separately, you'll get 2 - 1.99 which is TRUE. &amp;nbsp;But the difference between them is 0.011 which is FALSE. &amp;nbsp;Instead, take the absolute value of the difference and compare that to your tolerance level. &amp;nbsp;It's a little clumsy, but you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;length result $ 5;&lt;/P&gt;
&lt;P&gt;if x = y = . then result = 'TRUE';&lt;/P&gt;
&lt;P&gt;else if x=. then result = 'FALSE';&lt;/P&gt;
&lt;P&gt;else if y=. then result = 'FALSE';&lt;/P&gt;
&lt;P&gt;else do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if abs(x - y) &amp;lt;= tolerance then result='TRUE';&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else result='FALSE';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There may be slicker ways to code this, but stay away from ROUND.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Nice thing abou this is if you have a changing tolerance it is easier to use by supplying the tolerance value as part of the incoming data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might suggest a 1/0 coding instead of "True"/"False" as if you take the mean of the 1/0 then you get a percentage of in tolerance or sum a count of within tolerance not to mention use in some modeling approaches.&lt;/P&gt;
&lt;P&gt;With either approach I might suggest that x=. or y=. is missing instead of coded as "False" as someone could mistake that with the result of an actual comparison between two values.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Nov 2015 21:05:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-variables-for-equality-within-tolerance-niether/m-p/234710#M308515</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-11-13T21:05:41Z</dc:date>
    </item>
  </channel>
</rss>

