<?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/370957#M88599</link>
    <description>&lt;P&gt;Thanks, that will help!&lt;/P&gt;</description>
    <pubDate>Tue, 27 Jun 2017 14:41:00 GMT</pubDate>
    <dc:creator>Slash</dc:creator>
    <dc:date>2017-06-27T14:41:00Z</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/370899#M88574</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;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;&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;&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:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370899#M88574</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-06-27T13:09:24Z</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/370940#M88589</link>
      <description>&lt;P&gt;for your first question&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;The order of variable SCORE in Data Set RESULT has changed:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&lt;STRONG&gt;Order is not preserved in a sql join. So results are pretty as&amp;nbsp;much expected.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 14:05:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370940#M88589</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-27T14:05: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/370941#M88590</link>
      <description>&lt;P&gt;TL;DR;&lt;/P&gt;
&lt;P&gt;Anwyays from your top bit, SQL is intrinsically an unsorted procedure - i.e. data does not need to be sorted when passed into it (unlike SAS procedures). &amp;nbsp;When the SQL parser runs it creates sort blocks, merge blocks etc. in what is called the SQL plan. &amp;nbsp;This is bigger than the code you write. &amp;nbsp;If you don't explicitly state certian things, such as output order, then it is left to the SQL parser to decide on what to do. &amp;nbsp;You can see what methods are chose using the _method option and see the whole process using _tree:&lt;/P&gt;
&lt;PRE&gt;proc sql _method _tree; 
...
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2017 14:06:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370941#M88590</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-06-27T14:06:04Z</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/370957#M88599</link>
      <description>&lt;P&gt;Thanks, that will help!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 14:41:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370957#M88599</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-06-27T14:41:00Z</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/370959#M88600</link>
      <description>&lt;P&gt;Thx.&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 14:41:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370959#M88600</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-06-27T14:41:52Z</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/370964#M88604</link>
      <description>&lt;P&gt;For added fun examine LEFT JOIN&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
	create table result2 as
	select a.*,b.score
	from table1 a left join table2 b
	on a.id=b.id;
quit;&lt;/PRE&gt;
&lt;P&gt;and compare the results with inner join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically if you care about the resulting order include an ORDER BY clause to get what you want.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2017 15:05:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/370964#M88604</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-27T15:05:49Z</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/371288#M88685</link>
      <description>&lt;P&gt;Thx, the order by clause is needed.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jun 2017 13:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/The-order-of-SQL-Join-Result/m-p/371288#M88685</guid>
      <dc:creator>Slash</dc:creator>
      <dc:date>2017-06-28T13:28:21Z</dc:date>
    </item>
  </channel>
</rss>

