<?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: How to get value closest to another variable from a series in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720526#M223207</link>
    <description>&lt;P&gt;I think this does what you are looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array c (*) cause effect;
   comp =c [ whichn(min(abs(tally-cause),abs(tally-effect)),abs( tally-cause),abs( tally-effect) ) ];
run;&lt;/PRE&gt;
&lt;P&gt;The WHICHN function compares the first value, in this case the minimum absolute difference between the two values, and returns the position of the value in the other other parameters or a zero. An array provides a way to use that ordered value to select the original value you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: if you have missing values for Tally this doesn't work. If both cause and effect are missing this also will not work and will cause "ERROR: Array subscript out of range" and not execute. If you have missing values that may do that you need to test and not execute the Comp= line.&lt;/P&gt;</description>
    <pubDate>Fri, 19 Feb 2021 17:47:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-02-19T17:47:54Z</dc:date>
    <item>
      <title>How to get value closest to another variable from a series</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720521#M223203</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id cause effect tally;
datalines;
1 91 80 30      
2 23 85 28
3 34 86 95
4 15 45 25 
5 28 87 24
6 10 90 28
7 23 95 28
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi Community,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a requirement to get the value closest to tally and a new variable named COMP must be created. For ex. for ID = 1 then 30 should be compared to 91 and 80 and as 80 is closest 30 - COMP = 80.&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;id cause effect tally COMP 1 91 80 30 80 2 23 85 28 23 3 34 86 95 86 4 15 45 25 15 5 28 87 24 28 6 10 90 28 10 7 23 95 28&amp;nbsp;23&lt;/CODE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; Any ideas??&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2021 17:33:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720521#M223203</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2021-02-19T17:33:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to get value closest to another variable from a series</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720523#M223204</link>
      <description>&lt;P&gt;So you have two values, cause and effect and you want to see which is closes to the tally variable within the same row?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take the difference and then use the smaller one with IF statements seems like the simplest solution (untested).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;

dif_tally_cause = tally - cause;
dif_tally_effect = tally -effect;

*verify the logic here, didn't spend any time testing;
if diff_tally_cause &amp;gt; diff_tally_effect then comp = effect;
else comp = tally;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to factor in the direction of the difference use the ABS() function in the IF statement.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if abs(diff_tally_cause) &amp;gt; abs(diff_tally_effect) then comp = effect;
else comp = tally;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/110575"&gt;@shasank&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id cause effect tally;
datalines;
1 91 80 30      
2 23 85 28
3 34 86 95
4 15 45 25 
5 28 87 24
6 10 90 28
7 23 95 28
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi Community,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a requirement to get the value closest to tally and a new variable named COMP must be created. For ex. for ID = 1 then 30 should be compared to 91 and 80 and as 80 is closest 30 - COMP = 80.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;id cause effect tally COMP&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;1 91 80 30 80 2 23 85 28 23 3 34 86 95 86 4 15 45 25 15 5 28 87 24 28 6 10 90 28 10 7 23 95 28&amp;nbsp; &amp;nbsp;23&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Any ideas??&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2021 17:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720523#M223204</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-19T17:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to get value closest to another variable from a series</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720524#M223205</link>
      <description>Thank you Reeza. Correct. Cause and Effect should be compared to tally and get the closest. But, I have 7 variable that should be compared to tally. So, was asking to see if there is an advanced way.</description>
      <pubDate>Fri, 19 Feb 2021 17:40:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720524#M223205</guid>
      <dc:creator>shasank</dc:creator>
      <dc:date>2021-02-19T17:40:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to get value closest to another variable from a series</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720526#M223207</link>
      <description>&lt;P&gt;I think this does what you are looking for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   array c (*) cause effect;
   comp =c [ whichn(min(abs(tally-cause),abs(tally-effect)),abs( tally-cause),abs( tally-effect) ) ];
run;&lt;/PRE&gt;
&lt;P&gt;The WHICHN function compares the first value, in this case the minimum absolute difference between the two values, and returns the position of the value in the other other parameters or a zero. An array provides a way to use that ordered value to select the original value you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: if you have missing values for Tally this doesn't work. If both cause and effect are missing this also will not work and will cause "ERROR: Array subscript out of range" and not execute. If you have missing values that may do that you need to test and not execute the Comp= line.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Feb 2021 17:47:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-value-closest-to-another-variable-from-a-series/m-p/720526#M223207</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-02-19T17:47:54Z</dc:date>
    </item>
  </channel>
</rss>

