<?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 Reflexive join but limit some of the joining in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255561#M48815</link>
    <description>&lt;P&gt;I have a constructed a reflexive join but I want to limit some of the process to reduce overhead and get back only the information I need/want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 infile datalines delimiter = ',';
 input serial lat long date;
 format date date9.;
 datalines;
 1, 58.6732, 118.2569, 20225
 1, 58.6733, 118.2568, 20226
 1, 58.9865, 118.3156, 20227
 1, 75.259,  -52.0023, 20228
 1, 75.2689, -52.125,  20229
 2, 10.123,  15.256,   20111
 2, 10.2365, 14.2698,  20112
 2, 11.3569, 15.3489,  20113
 3, 23.2569, 0.2659,   20256
 3, 22.6789, 0.2788,   20257
 3, 12.3569, 35.126,   20258
 3, 12.3789, 34.679,   20259
 ;
run;

data test;
	set test;
	i = _n_;
run;

%let rad = constant('pi') / 180;  /*degrees to radians*/
%let r_km = 6371;                 /*radius of the earth in km (3959 miles)*/

proc fcmp outlib = work.funcs.haversine;
	function haversine(lat1, long1, lat2, long2);  /*function name*/
		/*function definition*/
		*dist = &amp;amp;r_km * 2 * arsin(min(1, sqrt(sin((lat2 - lat1) * &amp;amp;rad / 2)**2 + 
		cos(lat1 * &amp;amp;rad) * cos(lat2 * &amp;amp;rad) * sin((long2 - long1) * &amp;amp;rad / 2)**2)));
		/*asin should be equiv to atan2*/
		difflat = lat2 - lat1;
		difflong = long2 - long1;
		arg = sin(difflat / 2 * &amp;amp;rad)**2 + cos(lat1 * &amp;amp;rad) * cos(lat2 * &amp;amp;rad) * sin(difflong / 2 * &amp;amp;rad)**2;
		dist = 2 * &amp;amp;r_km * atan2(sqrt(arg), sqrt(1 - arg));
		return(dist);  /*what to return*/
	endsub;
run;
options cmplib = work.funcs; /*needed to access the function*/

proc sql _method;
	create table results (drop = sn date1 j) 
	as select
		a.*,
		b.serial as sn label = 'sn',
		b.lat as lat1 label = 'lat1',
		b.long as long1 label = 'long1',
		b.date as date1 label = 'date1',
		b.i as j,
		haversine(a.lat, a.long, b.lat, b.long) as hdist
	from test as a
	inner join test as b
	on a.serial = b.serial and a.i &amp;gt; b.i;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I added a.i &amp;gt; b.i to remove the symmetric results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Is SAS performing the computation and then removing it or is it not executing it all a.i &amp;gt; b.i cases?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;How could I trim the cases down to just, for serial 1, dist of 1 with 2, 2, with 3, .... and not run 1 with 3, 1 with 4, etc.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Mar 2016 15:50:06 GMT</pubDate>
    <dc:creator>dwsmith</dc:creator>
    <dc:date>2016-03-09T15:50:06Z</dc:date>
    <item>
      <title>Reflexive join but limit some of the joining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255561#M48815</link>
      <description>&lt;P&gt;I have a constructed a reflexive join but I want to limit some of the process to reduce overhead and get back only the information I need/want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 infile datalines delimiter = ',';
 input serial lat long date;
 format date date9.;
 datalines;
 1, 58.6732, 118.2569, 20225
 1, 58.6733, 118.2568, 20226
 1, 58.9865, 118.3156, 20227
 1, 75.259,  -52.0023, 20228
 1, 75.2689, -52.125,  20229
 2, 10.123,  15.256,   20111
 2, 10.2365, 14.2698,  20112
 2, 11.3569, 15.3489,  20113
 3, 23.2569, 0.2659,   20256
 3, 22.6789, 0.2788,   20257
 3, 12.3569, 35.126,   20258
 3, 12.3789, 34.679,   20259
 ;
run;

data test;
	set test;
	i = _n_;
run;

%let rad = constant('pi') / 180;  /*degrees to radians*/
%let r_km = 6371;                 /*radius of the earth in km (3959 miles)*/

proc fcmp outlib = work.funcs.haversine;
	function haversine(lat1, long1, lat2, long2);  /*function name*/
		/*function definition*/
		*dist = &amp;amp;r_km * 2 * arsin(min(1, sqrt(sin((lat2 - lat1) * &amp;amp;rad / 2)**2 + 
		cos(lat1 * &amp;amp;rad) * cos(lat2 * &amp;amp;rad) * sin((long2 - long1) * &amp;amp;rad / 2)**2)));
		/*asin should be equiv to atan2*/
		difflat = lat2 - lat1;
		difflong = long2 - long1;
		arg = sin(difflat / 2 * &amp;amp;rad)**2 + cos(lat1 * &amp;amp;rad) * cos(lat2 * &amp;amp;rad) * sin(difflong / 2 * &amp;amp;rad)**2;
		dist = 2 * &amp;amp;r_km * atan2(sqrt(arg), sqrt(1 - arg));
		return(dist);  /*what to return*/
	endsub;
run;
options cmplib = work.funcs; /*needed to access the function*/

proc sql _method;
	create table results (drop = sn date1 j) 
	as select
		a.*,
		b.serial as sn label = 'sn',
		b.lat as lat1 label = 'lat1',
		b.long as long1 label = 'long1',
		b.date as date1 label = 'date1',
		b.i as j,
		haversine(a.lat, a.long, b.lat, b.long) as hdist
	from test as a
	inner join test as b
	on a.serial = b.serial and a.i &amp;gt; b.i;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I added a.i &amp;gt; b.i to remove the symmetric results.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Is SAS performing the computation and then removing it or is it not executing it all a.i &amp;gt; b.i cases?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;How could I trim the cases down to just, for serial 1, dist of 1 with 2, 2, with 3, .... and not run 1 with 3, 1 with 4, etc.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 15:50:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255561#M48815</guid>
      <dc:creator>dwsmith</dc:creator>
      <dc:date>2016-03-09T15:50:06Z</dc:date>
    </item>
    <item>
      <title>Re: Reflexive join but limit some of the joining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255564#M48817</link>
      <description>&lt;P&gt;I can't tell quite what exact values you are subsetting but the generall approach would be to replace&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;inner join test as b&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;with something like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;inner join (select your variables go here from&amp;nbsp;test where serial=1 and dist=1)&amp;nbsp;as b&lt;/P&gt;
&lt;P&gt;or other conditions on the where. If they are complex enough it may be worth subsetting prior to the join to keep that code cleaner.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 15:58:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255564#M48817</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-03-09T15:58:13Z</dc:date>
    </item>
    <item>
      <title>Re: Reflexive join but limit some of the joining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255614#M48846</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;on a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;serial &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;serial and a&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;i = a.serial and b&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;i = a.i+1&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Mar 2016 18:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255614#M48846</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-09T18:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: Reflexive join but limit some of the joining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255617#M48847</link>
      <description>&lt;P&gt;Your solution only returns one item in the table which is not what I was asking.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Mar 2016 15:41:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255617#M48847</guid>
      <dc:creator>dwsmith</dc:creator>
      <dc:date>2016-03-10T15:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: Reflexive join but limit some of the joining</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255620#M48850</link>
      <description>&lt;P&gt;What is "&lt;SPAN&gt;the information I need/want&lt;/SPAN&gt;" ? Please post the expected result corresponding to your example data.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2016 19:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Reflexive-join-but-limit-some-of-the-joining/m-p/255620#M48850</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-03-09T19:03:36Z</dc:date>
    </item>
  </channel>
</rss>

