<?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: Difficulty with PROC SQL documentation--ORDER BY clause in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/458117#M70162</link>
    <description>&lt;P&gt;Very well, I believe that you and PGStats have solved it. Everything else&amp;nbsp;I have read or seen&amp;nbsp;about PROC SQL (or SQL in general) aligns with your interpretation. I appreciate the insights that you two shared.&lt;/P&gt;</description>
    <pubDate>Fri, 27 Apr 2018 13:22:58 GMT</pubDate>
    <dc:creator>Yosef</dc:creator>
    <dc:date>2018-04-27T13:22:58Z</dc:date>
    <item>
      <title>Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457808#M70153</link>
      <description>&lt;P&gt;I am using SAS 9.4. My question is, "When is it necessary to use an&amp;nbsp;ORDER BY clause&amp;nbsp;in PROC SQL?"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My question was inspired mostly by general statements about the PROC SQL&amp;nbsp;ORDER BY clause&amp;nbsp;in the SAS&amp;nbsp;9.4 documentation like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;"If an ORDER BY clause is omitted, then a particular order to the output rows, such as the order in which the rows are encountered in the queried table, cannot be guaranteed—even if an index is present. Without an ORDER BY clause, the order of the output rows is determined by the internal processing of PROC SQL, the default collating sequence of SAS, and your operating environment."&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;The documentation elaborates on these ideas:&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;DIV class="xis-paraSimpleFirst"&gt;&lt;LI-SPOILER&gt;The order of the output rows that are returned is guaranteed only for columns that are specified in the ORDER BY clause.&lt;DIV class="xis-note"&gt;&lt;SPAN class="xis-noteGenText"&gt;Note: &lt;/SPAN&gt;The ORDER BY clause does not guarantee that the order of the rows generated is deterministic. The ANSI standard for SQL allows the SQL implementation to specify whether the ORDER BY clause is stable or unstable. If the joint combination of values that is referenced in an ORDER BY clause for a query are unique in all of the rows that are being ordered, then the order of rows that is generated by ORDER BY is always deterministic. However, if the ORDER BY clause does not reference a joint combination of unique values, then the order of rows is not deterministic if ORDER BY is unstable.&lt;/DIV&gt;&lt;/LI-SPOILER&gt;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;I don't understand what&amp;nbsp;the documentation means and have not found any relevant articles. If my question is too general, then I have an example of PROC SQL code below that was very similar to something I did at my job recently. To me, it does not seem that the cautions in the documentation about order of rows not being deterministic applies to either of the two datasets I made using PROC SQL here, as the two variables in the ORDER BY clauses uniquely identify observations in the resulting datasets:&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*This sort is not necessary here, but the data I worked with had been sorted like so.;
proc sort data=sashelp.bmt out=sorted;
	by Group Status;
run;

proc sql;
	create table pre_want as
	select Group, Status, count(T) as freq_for_T
	from sorted
	group by Group, Status
	order by Group, Status;
	create table want as
	select *, count(Status) as freq_for_Status
	from pre_want
	group by Group
	order by Group, Status;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;The dataset "want" is here (with variable&amp;nbsp;labels removed):&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Group&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;Status&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;freq_for_T&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;&lt;STRONG&gt;freq_for_Status&lt;/STRONG&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ALL&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;14&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;ALL&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;24&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;AML-High Risk&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;11&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;AML-High Risk&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;34&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;AML-Low Risk&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;29&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;AML-Low Risk&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;25&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="xis-paraSimpleFirst"&gt;I am confused by the PROC SQL&amp;nbsp;documentation and because I have seen many, many examples&amp;nbsp;of users using PROC SQL without an ORDER BY clause (regardless of whether a GROUP BY clause was used).&amp;nbsp;When is it necessary to use an ORDER BY clause with PROC SQL?&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 26 Apr 2018 16:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457808#M70153</guid>
      <dc:creator>Yosef</dc:creator>
      <dc:date>2018-04-26T16:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457809#M70154</link>
      <description>&lt;P&gt;The SQL language does not guarantee the order of the output if you don't include an ORDER BY clause.&lt;/P&gt;
&lt;P&gt;In general SAS's implementation will produce ordered results when you use a GROUP BY statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if SAS is pushing the query into a remote database then that database might not return the observations in order without an ORDER BY clause.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 16:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457809#M70154</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-26T16:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457838#M70155</link>
      <description>&lt;P&gt;If you need your data to be ordered, then ask for it with an ORDER BY clause. That makes&amp;nbsp;your intentions&amp;nbsp;explicit and there is no cost if it is already ordered.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 17:36:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457838#M70155</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-26T17:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457899#M70156</link>
      <description>&lt;P&gt;Ok, thank you both for the quick replies. I am trying to reconcile your comments with this statement here from the SAS User's guide:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"The order of the output rows that are returned is guaranteed only for columns that are specified in the ORDER BY clause."&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which of these two interpretations is correct:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1.) "The order of the output rows that are returned is NOT guaranteed for the columns that are not specified in the ORDER BY clause, but is ONLY guaranteed for the columns that are specified&amp;nbsp;in the ORDER BY clause." (This is definitely not what I want to happen, ever!)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.) "The ORDER BY clause functions in the same manner as the BY statement in PROC SORT." (This IS what I want.)&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 19:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457899#M70156</guid>
      <dc:creator>Yosef</dc:creator>
      <dc:date>2018-04-26T19:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457912#M70157</link>
      <description>&lt;P&gt;Look at option EQUALS in the sort procedure. What the documentation says about proc SQL ORDER BY clause is that the sorting is done (sometimes) with option NOEQUALS.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 19:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457912#M70157</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-26T19:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457921#M70159</link>
      <description>&lt;P&gt;I think they are trying to say that if the ORDER BY variables do not uniquely identify each row then you cannot count on what order the "duplicates" well be sorted.&amp;nbsp; So if you have data that is uniquely identified by ID and DATE and you order by ID only then the order of the multiple dates within a single value of ID is not determined. You could run the same query twice and get different orders.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2018 19:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/457921#M70159</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-26T19:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: Difficulty with PROC SQL documentation--ORDER BY clause</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/458117#M70162</link>
      <description>&lt;P&gt;Very well, I believe that you and PGStats have solved it. Everything else&amp;nbsp;I have read or seen&amp;nbsp;about PROC SQL (or SQL in general) aligns with your interpretation. I appreciate the insights that you two shared.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 13:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Difficulty-with-PROC-SQL-documentation-ORDER-BY-clause/m-p/458117#M70162</guid>
      <dc:creator>Yosef</dc:creator>
      <dc:date>2018-04-27T13:22:58Z</dc:date>
    </item>
  </channel>
</rss>

