<?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 aggregate function error in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650229#M195004</link>
    <description>&lt;P&gt;Error message:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ERROR: Summary functions are restricted to the &lt;STRONG&gt;SELECT&lt;/STRONG&gt; and &lt;STRONG&gt;HAVING&lt;/STRONG&gt; clauses only.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You have a SUM(), a summary function, in your ORDER BY statement. In SAS you can use CALCULATED if you've calculated this&amp;nbsp;&lt;/SPAN&gt;elsewhere. ie calculated sum_lo = sum(cnt)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;47 proc sql;
48 create table lo as
49 SELECT DISTINCT LO_NAME,
50 Processing_Center_mlcs,
51 APP_DT as Year format mmddyy10.,
52 app_dt2 as Month format mmddyy10.,
53 Sum(cnt) AS Sum_Lo
54 FROM TBL2_DEDUP_4_
55 GROUP BY LO_NAME,Processing_Center_mlcs,
56 APP_DT,app_dt2,
57 &lt;FONT color="#000000"&gt;Year(APP_DT), Year(app_dt2)&lt;/FONT&gt;
58 ORDER BY &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;sum(cnt)&lt;/STRONG&gt;&lt;/FONT&gt; DESC
59 /*having sum(cnt)*/
60 /*ORDER BY Sum(cnt) DESC;*/
61 ;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Something like this maybe, did you intend to take year(app_dt2) rather than month? That seems wrong to me. FYI please paste&amp;nbsp;your code separately from your&amp;nbsp;log in the&amp;nbsp;future, it makes it easier to correct your code.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;47 proc sql;
48 create table lo as
49 SELECT DISTINCT LO_NAME,
50 Processing_Center_mlcs,
51 APP_DT as Year format mmddyy10.,
52 app_dt2 as Month format mmddyy10.,
53 Sum(cnt) AS Sum_Lo
54 FROM TBL2_DEDUP_4_
55 GROUP BY LO_NAME,Processing_Center_mlcs,
56 APP_DT,app_dt2,
57 calculated year, Year(app_dt2)
58 ORDER BY calculated sum_lo DESC
59 /*having sum(cnt)*/
60 /*ORDER BY Sum(cnt) DESC;*/
61 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 May 2020 00:17:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-05-25T00:17:01Z</dc:date>
    <item>
      <title>Proc Sql aggregate function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650225#M195002</link>
      <description>&lt;P&gt;47 proc sql;&lt;BR /&gt;48 create table lo as&lt;BR /&gt;49 SELECT DISTINCT LO_NAME,&lt;BR /&gt;50 Processing_Center_mlcs,&lt;BR /&gt;51 APP_DT as Year format mmddyy10.,&lt;BR /&gt;52 app_dt2 as Month format mmddyy10.,&lt;BR /&gt;53 Sum(cnt) AS Sum_Lo&lt;BR /&gt;54 FROM TBL2_DEDUP_4_&lt;BR /&gt;55 GROUP BY LO_NAME,Processing_Center_mlcs,&lt;BR /&gt;56 APP_DT,app_dt2,&lt;BR /&gt;57 Year(APP_DT), Year(app_dt2)&lt;BR /&gt;58 ORDER BY sum(cnt) DESC&lt;BR /&gt;59 /*having sum(cnt)*/&lt;BR /&gt;60 /*ORDER BY Sum(cnt) DESC;*/&lt;BR /&gt;61 ;&lt;BR /&gt;ERROR: Summary functions are restricted to the SELECT and HAVING clauses only.&lt;/P&gt;
&lt;P&gt;Why am I getting this error.&amp;nbsp; I copied the code from MS Access sql&lt;/P&gt;</description>
      <pubDate>Sun, 24 May 2020 23:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650225#M195002</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2020-05-24T23:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql aggregate function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650229#M195004</link>
      <description>&lt;P&gt;Error message:&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ERROR: Summary functions are restricted to the &lt;STRONG&gt;SELECT&lt;/STRONG&gt; and &lt;STRONG&gt;HAVING&lt;/STRONG&gt; clauses only.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You have a SUM(), a summary function, in your ORDER BY statement. In SAS you can use CALCULATED if you've calculated this&amp;nbsp;&lt;/SPAN&gt;elsewhere. ie calculated sum_lo = sum(cnt)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;47 proc sql;
48 create table lo as
49 SELECT DISTINCT LO_NAME,
50 Processing_Center_mlcs,
51 APP_DT as Year format mmddyy10.,
52 app_dt2 as Month format mmddyy10.,
53 Sum(cnt) AS Sum_Lo
54 FROM TBL2_DEDUP_4_
55 GROUP BY LO_NAME,Processing_Center_mlcs,
56 APP_DT,app_dt2,
57 &lt;FONT color="#000000"&gt;Year(APP_DT), Year(app_dt2)&lt;/FONT&gt;
58 ORDER BY &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;sum(cnt)&lt;/STRONG&gt;&lt;/FONT&gt; DESC
59 /*having sum(cnt)*/
60 /*ORDER BY Sum(cnt) DESC;*/
61 ;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Something like this maybe, did you intend to take year(app_dt2) rather than month? That seems wrong to me. FYI please paste&amp;nbsp;your code separately from your&amp;nbsp;log in the&amp;nbsp;future, it makes it easier to correct your code.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;47 proc sql;
48 create table lo as
49 SELECT DISTINCT LO_NAME,
50 Processing_Center_mlcs,
51 APP_DT as Year format mmddyy10.,
52 app_dt2 as Month format mmddyy10.,
53 Sum(cnt) AS Sum_Lo
54 FROM TBL2_DEDUP_4_
55 GROUP BY LO_NAME,Processing_Center_mlcs,
56 APP_DT,app_dt2,
57 calculated year, Year(app_dt2)
58 ORDER BY calculated sum_lo DESC
59 /*having sum(cnt)*/
60 /*ORDER BY Sum(cnt) DESC;*/
61 ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2020 00:17:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650229#M195004</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-25T00:17:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql aggregate function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650231#M195005</link>
      <description>&lt;P&gt;No need to call the aggregate function twice.&amp;nbsp; Since you already included the sum in the set of variables being selected just refer to that new variable in the ORDER BY clause.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select sex,count(*) as n
 from sashelp.class
 group by sex
 order by n desc
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If for some (?what possible?) reason you don't actually want to keep that variable even though you are using it for ordering use the DROP= dataset option.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table gender_by_count(drop=n) as
select sex,count(*) as n
 from sashelp.class
 group by sex
 order by n desc
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 May 2020 01:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650231#M195005</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-25T01:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Sql aggregate function error</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650263#M195008</link>
      <description>&lt;P&gt;Be extremely wary of assuming code that runs in one version of SQL will in any particular other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an older Access manual with a page bookmarked that has line similar to "there are over 250 standard SQL features not implemented in Access". Almost every version of SQL has extensions that won't work in others. Sometimes they are as simple as functions, sometimes they are useful but not standard tools for manipulating sets of data such as Partition.&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2020 01:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Sql-aggregate-function-error/m-p/650263#M195008</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-25T01:21:02Z</dc:date>
    </item>
  </channel>
</rss>

