<?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: proc sql order by in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491854#M129112</link>
    <description>&lt;P&gt;TOP 1 won't work in SAS SQL, that is invalid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also cannot use a function in ORDER BY, but you can use the variable you already created you just need to add the CALCULATED keyword in front of it, as demonstrated in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s&amp;nbsp;example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/220091"&gt;@deltaskipper&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In mysql&amp;nbsp;server this code run without error&lt;/P&gt;
&lt;P&gt;SELECT &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;TOP 1&lt;/FONT&gt; &lt;/STRONG&gt;COUNT(TRANSACTION_ID) AS NO_OF_TRANSACTION,STORE_TYPE&lt;BR /&gt;FROM TBL_TRANSACTION&lt;BR /&gt;GROUP BY store_type&lt;BR /&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but in sas&amp;nbsp;when using proc sql&amp;nbsp;the code:&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table delta as&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SELECT &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;TOP 1&lt;/STRONG&gt; &lt;/FONT&gt;COUNT(TRANSACTION_ID)&amp;nbsp;&lt;/SPAN&gt;AS&lt;SPAN&gt;&amp;nbsp;NO_OF_TRANSACTION,STORE_TYPE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FROM TBL_TRANSACTION&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GROUP BY store_type&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;shows an error&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="sasLog"&gt;Summary functions are restricted to the SELECT and HAVING clauses only&lt;/PRE&gt;
&lt;P&gt;why the same code run in mysql but not in sas&amp;nbsp;proc sql&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 01 Sep 2018 22:21:18 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-09-01T22:21:18Z</dc:date>
    <item>
      <title>proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491837#M129101</link>
      <description>&lt;P&gt;In mysql&amp;nbsp;server this code run without error&lt;/P&gt;&lt;P&gt;SELECT TOP 1 COUNT(TRANSACTION_ID) AS NO_OF_TRANSACTION,STORE_TYPE&lt;BR /&gt;FROM TBL_TRANSACTION&lt;BR /&gt;GROUP BY store_type&lt;BR /&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but in sas&amp;nbsp;when using proc sql&amp;nbsp;the code:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table delta as&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SELECT TOP 1 COUNT(TRANSACTION_ID)&amp;nbsp;&lt;/SPAN&gt;AS&lt;SPAN&gt;&amp;nbsp;NO_OF_TRANSACTION,STORE_TYPE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FROM TBL_TRANSACTION&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GROUP BY store_type&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;shows an error&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="sasLog"&gt;Summary functions are restricted to the SELECT and HAVING clauses only&lt;/PRE&gt;&lt;P&gt;why the same code run in mysql but not in sas&amp;nbsp;proc sql&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Sep 2018 17:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491837#M129101</guid>
      <dc:creator>deltaskipper</dc:creator>
      <dc:date>2018-09-01T17:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491843#M129105</link>
      <description>&lt;P&gt;Not sure why SAS sends that error since it is demonstrably false.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table delta as
  SELECT  age, COUNT(name) AS n 
  FROM sashelp.class 
  GROUP BY age
  ORDER BY calculated n desc
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Perhaps it means you cannot use them in ORDER BY unless they are ALSO included in the SELECT?&lt;/P&gt;</description>
      <pubDate>Sat, 01 Sep 2018 19:40:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491843#M129105</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-01T19:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491854#M129112</link>
      <description>&lt;P&gt;TOP 1 won't work in SAS SQL, that is invalid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You also cannot use a function in ORDER BY, but you can use the variable you already created you just need to add the CALCULATED keyword in front of it, as demonstrated in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;'s&amp;nbsp;example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/220091"&gt;@deltaskipper&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;In mysql&amp;nbsp;server this code run without error&lt;/P&gt;
&lt;P&gt;SELECT &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;TOP 1&lt;/FONT&gt; &lt;/STRONG&gt;COUNT(TRANSACTION_ID) AS NO_OF_TRANSACTION,STORE_TYPE&lt;BR /&gt;FROM TBL_TRANSACTION&lt;BR /&gt;GROUP BY store_type&lt;BR /&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but in sas&amp;nbsp;when using proc sql&amp;nbsp;the code:&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table delta as&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;SELECT &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;TOP 1&lt;/STRONG&gt; &lt;/FONT&gt;COUNT(TRANSACTION_ID)&amp;nbsp;&lt;/SPAN&gt;AS&lt;SPAN&gt;&amp;nbsp;NO_OF_TRANSACTION,STORE_TYPE&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FROM TBL_TRANSACTION&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GROUP BY store_type&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ORDER BY COUNT(TRANSACTION_ID) DESC&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;shows an error&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE class="sasLog"&gt;Summary functions are restricted to the SELECT and HAVING clauses only&lt;/PRE&gt;
&lt;P&gt;why the same code run in mysql but not in sas&amp;nbsp;proc sql&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Sep 2018 22:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491854#M129112</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-01T22:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491865#M129121</link>
      <description>&lt;P&gt;The TOP predicate is not supported by SAS/SQL. Here are two ways to get the most abundant age&amp;nbsp;in sashelp.class&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql outobs=1;
create table delta1 as
SELECT age, COUNT(name) AS n 
FROM sashelp.class 
GROUP BY age
ORDER BY calculated n desc;
quit;


proc sql;
create table delta2 as
SELECT age, n 
FROM (
    select age, count(name) as n 
    FROM sashelp.class 
    GROUP BY age )
HAVING n = max(n);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The second query&amp;nbsp;may return more than one record.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Sep 2018 03:01:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491865#M129121</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-02T03:01:46Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491868#M129124</link>
      <description>&lt;P&gt;Thanks for your help.&lt;/P&gt;&lt;P&gt;I really appreciate it.&lt;/P&gt;</description>
      <pubDate>Sun, 02 Sep 2018 05:21:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491868#M129124</guid>
      <dc:creator>deltaskipper</dc:creator>
      <dc:date>2018-09-02T05:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql order by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491869#M129125</link>
      <description>Thank you.</description>
      <pubDate>Sun, 02 Sep 2018 05:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-order-by/m-p/491869#M129125</guid>
      <dc:creator>deltaskipper</dc:creator>
      <dc:date>2018-09-02T05:21:57Z</dc:date>
    </item>
  </channel>
</rss>

