<?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 Finding Closest Value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65683#M14266</link>
    <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a requirement in which I should find closest value of amount.&lt;BR /&gt;
&lt;BR /&gt;
Ex:&lt;BR /&gt;
Amount  col1  col2  col3 col4 col5 col6 col7 col8 col9 col0 -------col100&lt;BR /&gt;
   10        11    12    13   56     60    78   87     9    11    14  ------          80&lt;BR /&gt;
&lt;BR /&gt;
Now I should get value from Col8 as it is 9 and closest to 10 (always closest value that is less than the amount value).&lt;BR /&gt;
Please let me know how can I achieve this. I have a table of 100,000 from which&lt;BR /&gt;
i should find closest value of amount in each row.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in Advance</description>
    <pubDate>Mon, 08 Mar 2010 17:11:25 GMT</pubDate>
    <dc:creator>SASACC</dc:creator>
    <dc:date>2010-03-08T17:11:25Z</dc:date>
    <item>
      <title>Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65683#M14266</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
I have a requirement in which I should find closest value of amount.&lt;BR /&gt;
&lt;BR /&gt;
Ex:&lt;BR /&gt;
Amount  col1  col2  col3 col4 col5 col6 col7 col8 col9 col0 -------col100&lt;BR /&gt;
   10        11    12    13   56     60    78   87     9    11    14  ------          80&lt;BR /&gt;
&lt;BR /&gt;
Now I should get value from Col8 as it is 9 and closest to 10 (always closest value that is less than the amount value).&lt;BR /&gt;
Please let me know how can I achieve this. I have a table of 100,000 from which&lt;BR /&gt;
i should find closest value of amount in each row.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in Advance</description>
      <pubDate>Mon, 08 Mar 2010 17:11:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65683#M14266</guid>
      <dc:creator>SASACC</dc:creator>
      <dc:date>2010-03-08T17:11:25Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65684#M14267</link>
      <description>Not sure if there are any functions to make this easy but a brute force way using arrays is.&lt;BR /&gt;
&lt;BR /&gt;
1 make an array of col1-col100&lt;BR /&gt;
2 if col(i) gt amount then col(i)=0&lt;BR /&gt;
3 closest=max( of col1-col100)&lt;BR /&gt;
&lt;BR /&gt;
but this is only good if there is atleast one value less than amount and I understand you correctly about wanting the closest value without going over.&lt;BR /&gt;
&lt;BR /&gt;
Message was edited by: RickM</description>
      <pubDate>Mon, 08 Mar 2010 17:24:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65684#M14267</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-03-08T17:24:05Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65685#M14268</link>
      <description>On second thought, in step 2 it would probably be better to set col(i) to .</description>
      <pubDate>Mon, 08 Mar 2010 17:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65685#M14268</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-03-08T17:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65686#M14269</link>
      <description>Hi, the ORDINAL function may be used to do this, but I don't think it is a better method if you have a large amount of variables.  &lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
	input amount col1-col5;&lt;BR /&gt;
cards;&lt;BR /&gt;
10 11 18 19 9 8&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
data want(drop=i);&lt;BR /&gt;
	set have;&lt;BR /&gt;
	do i=1 to 6;&lt;BR /&gt;
		if i&amp;gt;1 and ordinal(i,of amount--col5)=amount then do;&lt;BR /&gt;
			closest=ordinal(i-1,of amount--col5);&lt;BR /&gt;
			continue;&lt;BR /&gt;
		end;&lt;BR /&gt;
	end;&lt;BR /&gt;
run;</description>
      <pubDate>Tue, 09 Mar 2010 04:44:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65686#M14269</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-03-09T04:44:09Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65687#M14270</link>
      <description>Thanks for the replies. &lt;BR /&gt;
&lt;BR /&gt;
Looking for some more ideas. Many Thanks...</description>
      <pubDate>Tue, 09 Mar 2010 10:48:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65687#M14270</guid>
      <dc:creator>SASACC</dc:creator>
      <dc:date>2010-03-09T10:48:59Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65688#M14271</link>
      <description>try this, within your data step[pre] &lt;BR /&gt;
  array col(50) ;&lt;BR /&gt;
     minG = 9e9 ;&lt;BR /&gt;
     item = . ;&lt;BR /&gt;
     do _i_ = 1 to dim(cols) ;&lt;BR /&gt;
        gap =  amount - cols(_i_) ;&lt;BR /&gt;
        if gap &amp;lt; 0 then continue ;&lt;BR /&gt;
        if minG &amp;gt; gap then do;&lt;BR /&gt;
           minG = gap ;&lt;BR /&gt;
           item = _i_  ;&lt;BR /&gt;
        end ;&lt;BR /&gt;
     end ;&lt;BR /&gt;
     putlog 'closest to amount ' amount ' is ' item= cols(item)= &lt;BR /&gt;
[/pre]&lt;BR /&gt;
Of course, if you have SAS/IML available, then it could be performed in proc iml with matrix operations instead of array element handling. The syntax would be a whole lot more brief.&lt;BR /&gt;
 &lt;BR /&gt;
Peter</description>
      <pubDate>Wed, 10 Mar 2010 08:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65688#M14271</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2010-03-10T08:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65689#M14272</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Think this will work for you, just would need to extend the counter (i.e., 10) to the number of columns.&amp;nbsp; Also, if there is not lower record, you get -999999999.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data Clsst_Amnt_Dt;&lt;BR /&gt;infile datalines delimiter='09'x dsd firstobs=1; &lt;BR /&gt;input Amount col1 col2 col3 col4 col5 col6 col7 col8 col9 col10;&lt;BR /&gt;datalines;&lt;BR /&gt;10 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;11 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;84 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;62 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;4 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;57 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;1 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;8 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;88 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;14 8 12 13 56 60 78 87 9 11 5&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro ClsstAmnt;&lt;BR /&gt;Data Clsst_Amnt;&lt;BR /&gt;&amp;nbsp; Set Clsst_Amnt_Dt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ClsstAmntChk=-999999999;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; %do i=1 %to 10;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if col&amp;amp;i-Amount &amp;lt; 0 then&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if col&amp;amp;i-Amount &amp;gt; ClsstAmntChk then &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClsstAmntChk = col&amp;amp;i-Amount;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClsstAmnt = col&amp;amp;i;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;run;&lt;BR /&gt;%Mend;&lt;BR /&gt;%ClsstAmnt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 May 2015 12:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65689#M14272</guid>
      <dc:creator>Bryan</dc:creator>
      <dc:date>2015-05-11T12:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: Finding Closest Value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65690#M14273</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE&gt;

data Clsst_Amnt_Dt;
input Amount col1 col2 col3 col4 col5 col6 col7 col8 col9 col10;
datalines;
10 8 12 13 56 60 78 87 9 11 5
11 8 12 13 56 60 78 87 9 11 5
84 8 12 13 56 60 78 87 9 11 5
62 8 12 13 56 60 78 87 9 11 5
4 8 12 13 56 60 78 87 9 11 5
57 8 12 13 56 60 78 87 9 11 5
1 8 12 13 56 60 78 87 9 11 5
8 8 12 13 56 60 78 87 9 11 5
88 8 12 13 56 60 78 87 9 11 5
14 8 12 13 56 60 78 87 9 11 5
;
run;
data want;
 set Clsst_Amnt_Dt;
 array x{*}Amount col1-col10;
 array y{*}_Amount _col1-_col10;
 do i=1 to dim(x);
&amp;nbsp; y{i}=x{i};
 end;
call sortn(of y{*});
 do i=1 to dim(x);
&amp;nbsp; if y{i}=Amount then do;j=i-1;if j ne 0 then min=y{j};leave;end;
 end;
 drop i _:;
run;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 May 2015 15:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-Closest-Value/m-p/65690#M14273</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-05-11T15:36:43Z</dc:date>
    </item>
  </channel>
</rss>

