<?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 find the list of the K nearest neighbors in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24258#M854</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; One quick way to find nearest neighbors in a large set of observations is &lt;STRONG&gt;PROC MODECLUS&lt;/STRONG&gt;. Your specific problem could be solved by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote"&gt;&lt;P&gt;DATA observations;&lt;BR /&gt;do ID=1 TO 100;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; x1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1); &lt;BR /&gt;&amp;nbsp;&amp;nbsp; x2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; x3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA position;&lt;BR /&gt;ID = -999;&lt;BR /&gt;x1 = 0.5;&lt;BR /&gt;x2 = 0.5;&lt;BR /&gt;x3 = 0.5;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table test as&lt;BR /&gt;(select * from position)&lt;BR /&gt;union all&lt;BR /&gt;(select * from observations)&lt;BR /&gt;order by ID;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ods _all_ close;&lt;BR /&gt;proc modeclus data=test dk=11 /* = 10 observations + 1 position */ neighbor;&lt;BR /&gt;var x1 x2 x3;&lt;BR /&gt;id ID;&lt;BR /&gt;ods output Neighbor=nTest;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;ods listing;&lt;/P&gt;&lt;P&gt;proc sql inobs=10;&lt;BR /&gt;select nBor as ID, distance from nTest;&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 05 Oct 2011 16:56:38 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2011-10-05T16:56:38Z</dc:date>
    <item>
      <title>How to find the list of the K nearest neighbors</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24256#M852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I’m searching a &lt;EM&gt;proc &lt;/EM&gt;in sas to find the &lt;EM&gt;K nearest neighbors &lt;/EM&gt;in a set of points.&lt;/P&gt;&lt;P&gt;In the following data step, I generate a random set of 100 observations. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA observations;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do ID=1 TO 100;&amp;nbsp; &lt;/P&gt;&lt;P&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; x1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1); &lt;/P&gt;&lt;P&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; x2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;/P&gt;&lt;P&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; x3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;For example, with this set of 100 observations, is there a &lt;EM&gt;proc &lt;/EM&gt;to search the 10 nearest neighbor (Euclidian distance) of the point [ 0.5 ; 0.5 ; 0.5 ]?&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please note that I already found &lt;EM&gt;proc discrim &lt;/EM&gt;&lt;EM&gt; &lt;/EM&gt;to apply a KNN classification. But I want the &lt;SPAN style="text-decoration: underline;"&gt;list&lt;/SPAN&gt; of the K nearest neighbor and not to make a classification.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thx!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Oct 2011 09:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24256#M852</guid>
      <dc:creator>Hoccha</dc:creator>
      <dc:date>2011-10-05T09:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the list of the K nearest neighbors</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24257#M853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Why not just code it yourself?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote"&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data observations;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; do ID=1 TO 100; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = RAND('NORMAL',0,1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = RAND('NORMAL',0,1); &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x3 = RAND('NORMAL',0,1);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc sql noprint ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; create table distance as &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select distinct id,x1,x2,x3,sqrt((x1-y1)**2 + (x2-y2)**2 + (x3-y3)**2) as distance&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from observations&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , (select 0.5 as y1,0.5 as y2, 0.5 as y3 from observations(obs=1) )&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; order by distance desc&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;data top10;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&amp;nbsp; set distance (obs=10);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;proc print; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Oct 2011 11:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24257#M853</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2011-10-05T11:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the list of the K nearest neighbors</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24258#M854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; One quick way to find nearest neighbors in a large set of observations is &lt;STRONG&gt;PROC MODECLUS&lt;/STRONG&gt;. Your specific problem could be solved by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __jive_macro_name="quote" class="jive_text_macro jive_macro_quote"&gt;&lt;P&gt;DATA observations;&lt;BR /&gt;do ID=1 TO 100;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; x1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1); &lt;BR /&gt;&amp;nbsp;&amp;nbsp; x2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; x3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = RAND('NORMAL',0,1);&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA position;&lt;BR /&gt;ID = -999;&lt;BR /&gt;x1 = 0.5;&lt;BR /&gt;x2 = 0.5;&lt;BR /&gt;x3 = 0.5;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table test as&lt;BR /&gt;(select * from position)&lt;BR /&gt;union all&lt;BR /&gt;(select * from observations)&lt;BR /&gt;order by ID;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ods _all_ close;&lt;BR /&gt;proc modeclus data=test dk=11 /* = 10 observations + 1 position */ neighbor;&lt;BR /&gt;var x1 x2 x3;&lt;BR /&gt;id ID;&lt;BR /&gt;ods output Neighbor=nTest;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;ods listing;&lt;/P&gt;&lt;P&gt;proc sql inobs=10;&lt;BR /&gt;select nBor as ID, distance from nTest;&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 05 Oct 2011 16:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24258#M854</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2011-10-05T16:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the list of the K nearest neighbors</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24259#M855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello Tom &amp;amp; PGStats, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;many thanks for your help !&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;based on the message from Tom, I have buil the following macro "listOfKNN":&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;data observations;&lt;BR /&gt;&amp;nbsp; do ID=1 TO 100000; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x1 = RAND('NORMAL',0,1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x2 = RAND('NORMAL',0,1); &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x3 = RAND('NORMAL',0,1);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro listOfKNN (obser , target, K, nameOutput);&lt;BR /&gt;%Let dim = %eval(%SYSFUNC(count(&amp;amp;target,%NRSTR( )))+1) ;&lt;/P&gt;&lt;P&gt;%let ttt =;&lt;BR /&gt;%DO ii = 1 %TO &amp;amp;dim; &lt;BR /&gt;&amp;nbsp; %let ttt = &amp;amp;ttt.(x&amp;amp;ii.-%scan(&amp;amp;target,&amp;amp;ii," "))**2;&lt;BR /&gt;&amp;nbsp; %IF &amp;amp;ii NE &amp;amp;dim %THEN &lt;BR /&gt;&amp;nbsp;&amp;nbsp; %let ttt = &amp;amp;ttt.+; &lt;BR /&gt;%END;&lt;/P&gt;&lt;P&gt;proc sql noprint ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table &amp;amp;nameOutput as &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select distinct *, sqrt(&amp;amp;ttt)as distance&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from &amp;amp;obser&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; order by distance;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;data &amp;amp;nameOutput;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set &amp;amp;nameOutput (obs=&amp;amp;K);&lt;BR /&gt;&amp;nbsp; drop distance;&lt;BR /&gt;run;&lt;BR /&gt;%mend listOfKNN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/*make a test*/&lt;/P&gt;&lt;P&gt;%listOfKNN( obser&amp;nbsp;&amp;nbsp; = observations, &lt;BR /&gt;&amp;nbsp;&amp;nbsp; target&amp;nbsp; = 0.5 1 1, &lt;BR /&gt;&amp;nbsp;&amp;nbsp; K&amp;nbsp;&amp;nbsp; =&amp;nbsp; 5, &lt;BR /&gt;&amp;nbsp;&amp;nbsp; nameOutput = out&lt;BR /&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;B.R.&lt;/P&gt;&lt;P&gt;Olivier &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Oct 2011 07:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/How-to-find-the-list-of-the-K-nearest-neighbors/m-p/24259#M855</guid>
      <dc:creator>Hoccha</dc:creator>
      <dc:date>2011-10-06T07:20:04Z</dc:date>
    </item>
  </channel>
</rss>

