<?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: SAS 'group by' query in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578354#M164017</link>
    <description>&lt;P&gt;The reference for the so-called summary functions in SQL can be found here:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=sqlproc&amp;amp;docsetTarget=n123fsko39j44pn16zlt087e1m2h.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=sqlproc&amp;amp;docsetTarget=n123fsko39j44pn16zlt087e1m2h.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Some illustrations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, sum(weight)
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "archetypal" use of group by with summary functions. Group by a category variable, and do analysis on numerical values.&lt;/P&gt;
&lt;P&gt;Now, if I omit the summary function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, weight
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this WARNING in the log:&lt;/P&gt;
&lt;PRE&gt;27         proc sql;
28         create table test as
29         select sex, weight
30         from sashelp.class
31         group by sex;
WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT clause nor the optional HAVING 
         clause of the associated table-expression referenced a summary function.
&lt;/PRE&gt;
&lt;P&gt;and if I forget to add a further variable in the group by after adding it to the select:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, age, avg(weight)
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this in the log:&lt;/P&gt;
&lt;PRE&gt;27         proc sql;
28         create table test as
29         select sex, age, avg(weight)
30         from sashelp.class
31         group by sex;
NOTE: The query requires remerging summary statistics back with the original data.
&lt;/PRE&gt;
&lt;P&gt;and this result:&lt;/P&gt;
&lt;PRE&gt;Sex       Age          
-----------------------
F          14  90.11111
F          12  90.11111
F          11  90.11111
F          13  90.11111
F          14  90.11111
F          15  90.11111
F          12  90.11111
F          13  90.11111
F          15  90.11111
M          16    108.95
M          12    108.95
M          14    108.95
M          12    108.95
M          15    108.95
M          14    108.95
M          13    108.95
M          11    108.95
M          15    108.95
M          12    108.95
&lt;/PRE&gt;
&lt;P&gt;But if I do it right:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, age, avg(weight)
from sashelp.class
group by sex, age;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this:&lt;/P&gt;
&lt;PRE&gt;Sex       Age          
-----------------------
F          11      50.5
F          12     80.75
F          13        91
F          14     96.25
F          15    112.25
M          11        85
M          12     103.5
M          13        84
M          14     107.5
M          15     122.5
M          16       150
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Aug 2019 11:59:04 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-08-01T11:59:04Z</dc:date>
    <item>
      <title>SAS 'group by' query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578335#M164006</link>
      <description>&lt;P&gt;For the following code below, what should it be "group by 1,2; " or "group by 1,2,3; " (i.e. is "ifc(b.check is null, 'No', 'Yes') as check length=3" considered as a variable to group by, or not)? Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Code:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;data ac.dataset2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set ac.test;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;retain check 'Yes';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;create table test_check as&lt;BR /&gt;&amp;nbsp; &amp;nbsp;select a.account_id,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b.default_date,&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ifc(b.check is null, 'No', 'Yes') as check length=3&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; from ac.dataset1 as a&lt;BR /&gt;&amp;nbsp; &amp;nbsp; left join ac.dataset2 as b&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;on a.account_id=b.account_id&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;group by &lt;FONT color="#FF0000"&gt;1,2&lt;/FONT&gt;;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;FONT color="#FF0000"&gt;&amp;nbsp;/*Is it "group by 1,2; " or "group by 1,2,3; "? */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 11:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578335#M164006</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-08-01T11:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'group by' query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578344#M164011</link>
      <description>&lt;P&gt;If you do not have a summary function in your SQL query, "group by" can't do anything that makes sense, so SAS (or SQL as such) will automatically convert it to an "order by" clause, and give you the respective NOTE in the log (Maxim 2: Read the Log!).&lt;/P&gt;
&lt;P&gt;So, for clarity's sake, you should be a good coder and use the order by on your own.&lt;/P&gt;
&lt;P&gt;If you include the third variable in this is up to you. Do you always want 'No' before 'Yes' or not?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 11:27:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578344#M164011</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-01T11:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'group by' query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578351#M164014</link>
      <description>Thanks for your help, can you give me examples of what you mean by "summary function" so that you can use "group by", as opposed to "order by" in my example?&lt;BR /&gt;&lt;BR /&gt;My example has all of the records having 'Yes' for the check variable in the ac.dataset2 dataset, so that when it is left joined in the proc sql statement, there will be a comparison where account_ID's that match will receive 'Yes' for the check variable, and the answer will be 'No' when the account ID's do not match. The question I had is whether this "check" variable from the line would need to included in the "order by" statement, so do I use "order by 1,2; " or "order by 1,2,3; "?&lt;BR /&gt;&lt;BR /&gt;data ac.dataset2;&lt;BR /&gt;set ac.test;&lt;BR /&gt;retain check 'Yes';&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table test_check as&lt;BR /&gt;select a.account_id,&lt;BR /&gt;b.default_date,&lt;BR /&gt;ifc(b.check is null, 'No', 'Yes') as check length=3&lt;BR /&gt;from ac.dataset1 as a&lt;BR /&gt;left join ac.dataset2 as b&lt;BR /&gt;on a.account_id=b.account_id&lt;BR /&gt;order by 1,2;&lt;BR /&gt;quit;</description>
      <pubDate>Thu, 01 Aug 2019 11:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578351#M164014</guid>
      <dc:creator>jeremy4</dc:creator>
      <dc:date>2019-08-01T11:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS 'group by' query</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578354#M164017</link>
      <description>&lt;P&gt;The reference for the so-called summary functions in SQL can be found here:&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=sqlproc&amp;amp;docsetTarget=n123fsko39j44pn16zlt087e1m2h.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=sqlproc&amp;amp;docsetTarget=n123fsko39j44pn16zlt087e1m2h.htm&amp;amp;locale=en&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Some illustrations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, sum(weight)
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "archetypal" use of group by with summary functions. Group by a category variable, and do analysis on numerical values.&lt;/P&gt;
&lt;P&gt;Now, if I omit the summary function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, weight
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this WARNING in the log:&lt;/P&gt;
&lt;PRE&gt;27         proc sql;
28         create table test as
29         select sex, weight
30         from sashelp.class
31         group by sex;
WARNING: A GROUP BY clause has been transformed into an ORDER BY clause because neither the SELECT clause nor the optional HAVING 
         clause of the associated table-expression referenced a summary function.
&lt;/PRE&gt;
&lt;P&gt;and if I forget to add a further variable in the group by after adding it to the select:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, age, avg(weight)
from sashelp.class
group by sex;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this in the log:&lt;/P&gt;
&lt;PRE&gt;27         proc sql;
28         create table test as
29         select sex, age, avg(weight)
30         from sashelp.class
31         group by sex;
NOTE: The query requires remerging summary statistics back with the original data.
&lt;/PRE&gt;
&lt;P&gt;and this result:&lt;/P&gt;
&lt;PRE&gt;Sex       Age          
-----------------------
F          14  90.11111
F          12  90.11111
F          11  90.11111
F          13  90.11111
F          14  90.11111
F          15  90.11111
F          12  90.11111
F          13  90.11111
F          15  90.11111
M          16    108.95
M          12    108.95
M          14    108.95
M          12    108.95
M          15    108.95
M          14    108.95
M          13    108.95
M          11    108.95
M          15    108.95
M          12    108.95
&lt;/PRE&gt;
&lt;P&gt;But if I do it right:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table test as
select sex, age, avg(weight)
from sashelp.class
group by sex, age;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get this:&lt;/P&gt;
&lt;PRE&gt;Sex       Age          
-----------------------
F          11      50.5
F          12     80.75
F          13        91
F          14     96.25
F          15    112.25
M          11        85
M          12     103.5
M          13        84
M          14     107.5
M          15     122.5
M          16       150
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2019 11:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-group-by-query/m-p/578354#M164017</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-08-01T11:59:04Z</dc:date>
    </item>
  </channel>
</rss>

