<?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: The Order of SQL JOIN Result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/384493#M91822</link>
    <description>&lt;P&gt;Thanks! That is very helpful.&lt;/P&gt;</description>
    <pubDate>Tue, 01 Aug 2017 04:22:32 GMT</pubDate>
    <dc:creator>Slash</dc:creator>
    <dc:date>2017-08-01T04:22:32Z</dc:date>
    <item>
      <title>The Order of SQL JOIN Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/370908#M88576</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;Hi, guys&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;Recently, I've met a strange problem. I can't understand that. Hopefully, you guys can explain to me. Here is it:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;Let's see an basic result:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 2 variables and 1 observation in table1 */
data table1;
	id="001";
	num=uniform(1);
run;

/* 2 variables and 5 observation in table2*/
data table2;
   	input id $ score;
cards;
001 80
001 80
001 90
001 60
001 85
;
run;

/* Inner Join table1 and table2*/
proc sql;
	create table result as
	select a.*,b.score
	from table1 a inner join table2 b
	on a.id=b.id;
quit;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;The Data Set Result is:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9899i8F50ED2946940F17/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="result.png" title="result.png" /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" size="3"&gt;Notice the order of variable SCORE, is just same as the original order in table2.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;But when the number of variables&amp;nbsp;in table1 increases, magical thing happened. Let's see:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 12 variables and 1 observation in table1 */
data table1;
	id="001";
	array num{10} ;
	do i=1 to 10;
		num{i}=uniform(i)+i;
	end;
run;

/* 2 variables and 5 observation in table2 */
data table2;
	input id $ score;
cards;
001 80
001 80
001 90
001 60
001 85
;
run;

/* Inner Join table1 and table2*/
proc sql;
	create table result as
	select a.*,b.score
	from table1 a inner join table2 b
	on a.id=b.id;
quit;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="courier new,courier" size="3"&gt;The order of variable SCORE in Data Set RESULT has changed:&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9900iF40D4443F8C9B8D3/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="result2.png" title="result2.png" /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;So, I'm confused. Why did it change?&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;Next, I run a test, to see the relation between the number of the variables in data set table1 and the order of varialbe SCORE in data set RESULT. Here is the code:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%macro create_table1(count);
%local i;
data table1;
	id="001";
	%do i=1 %to &amp;amp;count;
		num&amp;amp;i=uniform(&amp;amp;i);
	%end;
run;
%mend create_table1;


%macro test_join;

proc sql;
	drop table differ_all;
quit;

%local i;
%do i=1 %to 20;

	%create_table1(&amp;amp;i)

	proc sql;
		create table result&amp;amp;i as
		select a.*,b.score
		from table1 a inner join table2 b
		on a.id=b.id;
	quit;


	data differ&amp;amp;i;
		merge result(keep=score rename=(score=base)) result&amp;amp;i(keep=score rename=(score=new)) end=last;
		length score_base score_new $ 20 differ_flag $ 5;
		retain score_base score_new;
		score_base=catx(',',score_base,base);
		score_new=catx(',',score_new,new);
		

		if last then do;
			var_count=&amp;amp;i+1;/*plus the variable ID*/
			differ_flag=ifc(score_base=score_new,'true','false');
			output;
		end;
		keep var_count score_base score_new differ_flag;
	run;


	proc append base=differ_all data=differ&amp;amp;i force;
	run;
%end;

proc print data=differ_all;run;

%mend test_join;



data table1;
	id="001";
	num=uniform(1);
run;
data table2;
	input id $ score;
cards;
001 80
001 80
001 90
001 60
001 85
;
run;
proc sql;
	create table result as
	select a.*,b.score
	from table1 a inner join table2 b
	on a.id=b.id;
quit;


%test_join
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;And the result is:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/9901i0D572439F13E19CE/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="result3.png" title="result3.png" /&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;When the number of variables of data set table1 equals 11, then order of SCORE in join table has changed. I test a few times, it's the same.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier" size="3"&gt;So, anyone can tell me the reason? And I can't find anything&amp;nbsp;about that in SAS Help and Documents.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 13:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/370908#M88576</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-06-27T13:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: The Order of SQL JOIN Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/376278#M90297</link>
      <description>&lt;P&gt;See&amp;nbsp;&lt;STRONG&gt;SQL Joins -- The Long and The Short of It&lt;/STRONG&gt;, available at&amp;nbsp;&lt;A title="link to Tech Support note 553" href="https://support.sas.com/techsup/technote/ts553.html" target="_blank"&gt;https://support.sas.com/techsup/technote/ts553.html&lt;/A&gt;. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you add _method to the PROC SQL statement, you'll get notes in the log show the SQL execution methods chosen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The notes in the log for the first 9 iterations of your test_join macro look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; NOTE: SQL execution methods chosen are:
 
       sqxcrta
           sqxjhsh
               sqxsrc( WORK.TABLE2(alias = B) )
               sqxsrc( WORK.TABLE1(alias = A) )
 NOTE: Table WORK.RESULT1 created, with 5 rows and 3 columns.&lt;/PRE&gt;
&lt;P&gt;The notes in the log for the 10th through 20th iterations of your test_join macro look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; NOTE: SQL execution methods chosen are:
 
       sqxcrta
           sqxjhsh
               sqxsrc( WORK.TABLE1(alias = A) )
               sqxsrc( WORK.TABLE2(alias = B) )
 NOTE: Table WORK.RESULT10 created, with 5 rows and 12 columns.&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Jul 2017 22:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/376278#M90297</guid>
      <dc:creator>SuzanneDorinski</dc:creator>
      <dc:date>2017-07-15T22:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: The Order of SQL JOIN Result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/384493#M91822</link>
      <description>&lt;P&gt;Thanks! That is very helpful.&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2017 04:22:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-Order-of-SQL-JOIN-Result/m-p/384493#M91822</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-08-01T04:22:32Z</dc:date>
    </item>
  </channel>
</rss>

