<?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: Does PROC SQL's &amp;quot;Select Distinct&amp;quot; always output in order? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Does-PROC-SQL-s-quot-Select-Distinct-quot-always-output-in-order/m-p/797659#M313604</link>
    <description>&lt;P&gt;Probably, but it's also not a good idea to assume that it always will.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally, SQL is a set language (as in logical sets), and order doesn't matter for sets.&amp;nbsp; So the same query will sometimes reorder data and sometimes will not, depending on how the SQL optimizer decides to execute the query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you care about the order of the output data, you should always add an ORDER BY clause.&lt;/P&gt;</description>
    <pubDate>Mon, 21 Feb 2022 20:50:04 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-02-21T20:50:04Z</dc:date>
    <item>
      <title>Does PROC SQL's "Select Distinct" always output in order?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-PROC-SQL-s-quot-Select-Distinct-quot-always-output-in-order/m-p/797619#M313579</link>
      <description>&lt;P&gt;Hi community,&lt;/P&gt;
&lt;P&gt;Are the results of a PROC SQL's "Select distinct" always given in sorted order?&lt;/P&gt;
&lt;P&gt;Or are there data arrangements (maybe with indexes?) that would/could affect the order of the results?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't find any documentation where the operations behind "Distinct" are explicitely described&lt;/P&gt;
&lt;P&gt;I've performed some tests on indexes datasets but it seems that the results are always sorted.&lt;/P&gt;
&lt;P&gt;(I've never questionned that, but I am doing now)&lt;/P&gt;
&lt;P&gt;I'm not using any groups and the order variable I need is always the "distinct" variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class(index=(sex));
set sashelp.class;
run;
proc sort data=class force;by sex name;run;

proc sql;
select distinct age
from sashelp.class
;
*same results as;
select distinct age
from class
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'd be grateful if you could share some piece of information on this.&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 16:54:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-PROC-SQL-s-quot-Select-Distinct-quot-always-output-in-order/m-p/797619#M313579</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-02-21T16:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Does PROC SQL's "Select Distinct" always output in order?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Does-PROC-SQL-s-quot-Select-Distinct-quot-always-output-in-order/m-p/797659#M313604</link>
      <description>&lt;P&gt;Probably, but it's also not a good idea to assume that it always will.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally, SQL is a set language (as in logical sets), and order doesn't matter for sets.&amp;nbsp; So the same query will sometimes reorder data and sometimes will not, depending on how the SQL optimizer decides to execute the query.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you care about the order of the output data, you should always add an ORDER BY clause.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 20:50:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Does-PROC-SQL-s-quot-Select-Distinct-quot-always-output-in-order/m-p/797659#M313604</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-02-21T20:50:04Z</dc:date>
    </item>
  </channel>
</rss>

