<?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: Selecting closest value from two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709733#M218322</link>
    <description>Yes, these are geocoordinates (Long/Lat).</description>
    <pubDate>Wed, 06 Jan 2021 21:36:59 GMT</pubDate>
    <dc:creator>dwarden3</dc:creator>
    <dc:date>2021-01-06T21:36:59Z</dc:date>
    <item>
      <title>Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709674#M218278</link>
      <description>&lt;P&gt;Thank you in advance.&lt;/P&gt;&lt;P&gt;Table A:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; X&amp;nbsp; Y&amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp;-90 35&amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp;-90 37&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp;-90&amp;nbsp; 39&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table B:&amp;nbsp;&lt;/P&gt;&lt;P&gt;Area&amp;nbsp; X&amp;nbsp; Y&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-90 33&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-91 36&lt;/P&gt;&lt;P&gt;C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-90 38&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I query Table B to select the area that is closest (smallest difference between both x and y) for each observation in Table A such that I end up with:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; X&amp;nbsp; Y&amp;nbsp; Area&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table A is about 240k observations. There are about 18k observations(areas) in table B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: for the final table, I want the X/Y coords from table A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 18:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709674#M218278</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2021-01-06T18:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709676#M218280</link>
      <description>&lt;P&gt;Closest? How do you define that in this case? Euclidean distance? Sum of the differences? Sum of the absolute value of the differences? Or some other measure?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Can you give an example or three?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 18:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709676#M218280</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-01-06T18:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709678#M218281</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Depending of your definition of distance, you can modify the macro below :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro distance(x1, y1, x2, y2);

    sqrt((&amp;amp;x2-&amp;amp;x1)**2+(&amp;amp;y2-&amp;amp;y1)**2)

%mend;

proc sql noprint;
CREATE TABLE want AS
SELECT a.*, b.Area, %distance(a.X,a.Y, b.X, b.Y) AS DIST
FROM a, b
GROUP BY ID
HAVING %distance(a.X,a.Y, b.X, b.Y)=min(%distance(a.X,a.Y, b.X, b.Y))
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the number of observations will increase if there are several observation for the minimal distance.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 18:56:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709678#M218281</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2021-01-06T18:56:48Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709679#M218282</link>
      <description>&lt;P&gt;Is there any other information that would allow at least grouping?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The meaning of X? If that is a longitude in degrees and the range of values is large enough then 179.9 and -179.9 are "closer" than 179.9 and 175 and we need to know that to appropriately calculate distance.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 18:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709679#M218282</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-06T18:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709680#M218283</link>
      <description>&lt;P&gt;Euclidean distance would work. These are X/Y coordinates.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 18:57:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709680#M218283</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2021-01-06T18:57:55Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709695#M218291</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129334"&gt;@dwarden3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Euclidean distance would work. These are X/Y coordinates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Are these geocoodinates? As in latitude and longitude?&lt;/P&gt;
&lt;P&gt;I ask because there is a specific function, Geodist, to calculate geodetic distance or the length of a curve on the earths surface and simple Euclidean distance may not be precise enough if dealing with a large range of coordinates.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 20:03:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709695#M218291</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-06T20:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709733#M218322</link>
      <description>Yes, these are geocoordinates (Long/Lat).</description>
      <pubDate>Wed, 06 Jan 2021 21:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709733#M218322</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2021-01-06T21:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709768#M218339</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/129334"&gt;@dwarden3&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yes, these are geocoordinates (Long/Lat).&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then you could replace the %Distance in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;'s solution with Geodist (lat1,long1, lat2,long2). The latitude does need to come before the longitude in the parameter order but the pair position doesn't.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The default unit reported as the distance would be Kilometers. If you want the distance in miles then Geodist (lat1,long1, lat2,long2,'M')&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jan 2021 23:01:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/709768#M218339</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-06T23:01:41Z</dc:date>
    </item>
    <item>
      <title>Re: Selecting closest value from two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/710026#M218480</link>
      <description>This works. Sorry for the late reply. I have to break the dataset up to the county level for it to run, but the output is exactly what I need. Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30622"&gt;@gamotte&lt;/a&gt;</description>
      <pubDate>Thu, 07 Jan 2021 19:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selecting-closest-value-from-two-variables/m-p/710026#M218480</guid>
      <dc:creator>dwarden3</dc:creator>
      <dc:date>2021-01-07T19:59:31Z</dc:date>
    </item>
  </channel>
</rss>

