<?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: Group by and Having in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/567050#M159416</link>
    <description>It probably isn't super useful to respond to 3+ year old threads &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
    <pubDate>Tue, 18 Jun 2019 19:53:01 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-18T19:53:01Z</dc:date>
    <item>
      <title>Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248537#M46692</link>
      <description>&lt;P&gt;Using SAS 7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;New to proc sql. I don't quite understand what Proc sql: Group by and Having&amp;nbsp;will give me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example: dataset have&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="232"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;&lt;STRONG&gt;Name&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="64"&gt;&lt;STRONG&gt;Date&lt;/STRONG&gt;&lt;/TD&gt;
&lt;TD width="104"&gt;&lt;STRONG&gt;Amount Spend&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;John&lt;/TD&gt;
&lt;TD&gt;Dec-17&lt;/TD&gt;
&lt;TD&gt;15&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;John&lt;/TD&gt;
&lt;TD&gt;Oct-16&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Peter&lt;/TD&gt;
&lt;TD&gt;Sep-16&lt;/TD&gt;
&lt;TD&gt;70&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Peter&lt;/TD&gt;
&lt;TD&gt;Feb-14&lt;/TD&gt;
&lt;TD&gt;60&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Will the below code output John as he has Amount_Spend&amp;lt;10? Or will&amp;nbsp;only John record with Amount Spend=15 be output?&amp;nbsp;Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table filtered As select * from have
group by name
having Amount_Spend&amp;gt; =10;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 07 Feb 2016 08:10:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248537#M46692</guid>
      <dc:creator>apple</dc:creator>
      <dc:date>2016-02-07T08:10:03Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248547#M46695</link>
      <description>You need to use an aggregate function (sum) if you wish to filter on an aggregated value.</description>
      <pubDate>Sun, 07 Feb 2016 13:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248547#M46695</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-07T13:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248553#M46698</link>
      <description>&lt;P&gt;That's not a typical use of the HAVING clause, it's typically used to filter aggregate functions. In your case, it will work as the equivalent of a WHERE clause, so you should get everything except the first row in your results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example that you should be able to run, where it picks anyone who has a height greater than the avg height, where avg height is calculated for each sex.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *, mean(height) as avg_height
from sashelp.class
group by sex
having height&amp;gt;avg_height;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS. I'm assuming you're using SAS EG 7? SAS 7 isn't a valid software version.&lt;/P&gt;</description>
      <pubDate>Sun, 07 Feb 2016 13:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248553#M46698</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-02-07T13:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248554#M46699</link>
      <description>Just some SAS trivia: I think that there were a SAS7, but it was released to a very limited list of US customers who couldn't wait for V8.</description>
      <pubDate>Sun, 07 Feb 2016 14:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/248554#M46699</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-02-07T14:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/567048#M159415</link>
      <description>Having is used to filter grouped/aggregated numbers. In your example, amount_spend was not aggregated, so it will cause an error.&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table filtered As select name,sum(amount_spend) as Spent from have&lt;BR /&gt;group by name&lt;BR /&gt;having sum(Amount_Spend)&amp;gt; =10;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;This code will have John-&amp;gt;18 and Peter-&amp;gt;130.&lt;BR /&gt;&lt;BR /&gt;However:&lt;BR /&gt;proc sql;&lt;BR /&gt;create table filtered As select name,sum(amount_spend) as Spent from have&lt;BR /&gt;where amount_spend&amp;gt;=10&lt;BR /&gt;group by name&lt;BR /&gt;having sum(Amount_Spend)&amp;gt; =10;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;will give you John-&amp;gt;15 and Peter-&amp;gt;130. John's amount_spend = 3 was filtered out by the where clause.</description>
      <pubDate>Tue, 18 Jun 2019 19:34:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/567048#M159415</guid>
      <dc:creator>jffeudo86</dc:creator>
      <dc:date>2019-06-18T19:34:59Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: Group by and Having</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/567050#M159416</link>
      <description>It probably isn't super useful to respond to 3+ year old threads &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;</description>
      <pubDate>Tue, 18 Jun 2019 19:53:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-sql-Group-by-and-Having/m-p/567050#M159416</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-18T19:53:01Z</dc:date>
    </item>
  </channel>
</rss>

