<?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 having statement with count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254947#M48662</link>
    <description>&lt;P&gt;Could you post example output required from your first two test datasets, its not clear to me which one needs to have four records, and what is being diff'd. &amp;nbsp;It should be straightforward enough to update:&lt;/P&gt;
&lt;PRE&gt;data a;
  input  id $  var1 ;
cards;
1 5
1 12
1 8
1 20
;
run;
data b;
  input  id $  var2 ;
cards;
1 15  
1 1
1 4
1 2
;
run;
proc sql;
  create table WANT as
  select  A.ID,
          A.VAR1,
          B.VAR_COUNT
  from    A A
  left join (select ID,count(*) as VAR_COUNT from B group by ID) B
  on      A.ID=B.ID;
quit;&lt;/PRE&gt;</description>
    <pubDate>Mon, 07 Mar 2016 14:26:14 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-03-07T14:26:14Z</dc:date>
    <item>
      <title>PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254931#M48659</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could someone explain how to perform a count on a inner join &amp;nbsp;to meet a specific condition&amp;nbsp;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My understanding about using &amp;nbsp;HAVING&amp;nbsp; statement when doing a merge (for instance) is that it evaluates the condition or expression on the output of the proc sql. In other words on the ouput result of my merge .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I am performing a inner join merge and I would like to keep only&amp;nbsp;ID&amp;nbsp;having at least 3 observations&amp;nbsp;with&amp;nbsp;&amp;nbsp;difference &amp;gt; 10. &amp;nbsp; For simplicity I will use only one ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data a;
input  id $  var1 ;
cards;
1 5
1 12
1 8
1 20
;
run;



data b;
input  id $  var2 ;
cards;
1 15  
1 1
1 4
1 2
;
run;

&lt;BR /&gt;proc sql;&lt;BR /&gt; create table case1 as&lt;BR /&gt; select a.id,a.var1,var1-var2 as diff&lt;BR /&gt; from a as a inner join b as b&lt;BR /&gt; on a.id=b.id&lt;BR /&gt; having( diff gt 10);&lt;BR /&gt;quit;&lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I need to add a count in proc sql. So I have done this :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;&lt;BR /&gt; create table case2 as&lt;BR /&gt; select a.id,count(*)&lt;BR /&gt; from a as a inner join b as b&lt;BR /&gt; on a.id=b.id&lt;BR /&gt; group by a.id&lt;BR /&gt; having( count(var1-var2 gt 10) gt 3 );&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;But the issue is that count is eqaul to 10 &amp;nbsp;while it should be equal to 4 ! How should I do to have a row with a count value of &amp;nbsp;4 ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any clue ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;saskap&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 13:05:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254931#M48659</guid>
      <dc:creator>saskapa</dc:creator>
      <dc:date>2016-03-07T13:05:19Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254935#M48660</link>
      <description>&lt;P&gt;Having is for filtering on aggregate values, it has nothing to do with merge/join operations.&lt;/P&gt;
&lt;P&gt;If you add count() just to make use of having, your are out of bounds.&lt;/P&gt;
&lt;P&gt;Try to use calculated in the where instead, it's a filter on row level you want, no?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 13:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254935#M48660</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-07T13:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254937#M48661</link>
      <description>&lt;P&gt;Hi LinusH !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks yes it s a filtering on the rows. I guess could probably use a where statment for diff &amp;gt; 10 and then using a having (count).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Howver If making use of a count() with HAVING I'll be out of bounds .I do not undertand why here I can use a count () with HAVING &amp;nbsp;? &amp;nbsp;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data c;
input  id $  var2 ;
cards;
1 15  
2 1
2 3
3 2
;
run;
proc sql;
select id
from c
group by id
having (count( distinct var2) gt 1);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 13:52:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254937#M48661</guid>
      <dc:creator>saskapa</dc:creator>
      <dc:date>2016-03-07T13:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254947#M48662</link>
      <description>&lt;P&gt;Could you post example output required from your first two test datasets, its not clear to me which one needs to have four records, and what is being diff'd. &amp;nbsp;It should be straightforward enough to update:&lt;/P&gt;
&lt;PRE&gt;data a;
  input  id $  var1 ;
cards;
1 5
1 12
1 8
1 20
;
run;
data b;
  input  id $  var2 ;
cards;
1 15  
1 1
1 4
1 2
;
run;
proc sql;
  create table WANT as
  select  A.ID,
          A.VAR1,
          B.VAR_COUNT
  from    A A
  left join (select ID,count(*) as VAR_COUNT from B group by ID) B
  on      A.ID=B.ID;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Mar 2016 14:26:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254947#M48662</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-03-07T14:26:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254971#M48675</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8584"&gt;@saskapa﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Perhaps what you want is something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table case2 as
select a.id, sum(var1-var2 gt 10) as cnt
from a join b
on a.id=b.id
group by a.id
having cnt gt 3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You wrote "I would like to keep only ID having at least 3 observations with &amp;nbsp;difference &amp;gt; 10."&amp;nbsp;It is not quite clear in which table you would count these 3 observations and why you increased this lower bound to 4 later on.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you perform a 4-to-4 join ("many-to-many join") as with your sample datasets, PROC SQL will internally create a temporary table with 4&lt;FONT face="symbol"&gt;*&lt;/FONT&gt;4=16 observations (all combinations of one obs. from dataset A and one from dataset B).&amp;nbsp;All of these observations satisfy the ON condition and fall into the group defined by a.id=1. Therefore, counts of observations will refer to this table (and hence can range from 0 to 16).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expression &lt;/P&gt;
&lt;PRE&gt;count(var1-var2 gt 10)&lt;/PRE&gt;
&lt;P&gt;in your HAVING clause counts observations for which the Boolean expression &lt;FONT face="courier new,courier"&gt;var1-var2 gt 10&lt;/FONT&gt; has &lt;EM&gt;a non-missing value&lt;/EM&gt; (see &lt;A href="http://support.sas.com/documentation/cdl/en/sqlproc/69049/HTML/default/viewer.htm#n123fsko39j44pn16zlt087e1m2h.htm" target="_blank"&gt;documentation&lt;/A&gt;).&amp;nbsp;Regardless of the (numeric, possibly missing) values of VAR1 and VAR2, this expresssion is 0 or 1, but never missing. So, the count equals 16. However, &lt;EM&gt;you&lt;/EM&gt; want to count the observations where the Boolean expression is &lt;EM&gt;1&lt;/EM&gt;, i.e. where &lt;FONT face="courier new,courier"&gt;var1-var2 gt 10&lt;/FONT&gt; is true.&amp;nbsp;You can achieve this with the SUM function (see my suggested code above), which adds the 16 (0 or 1)-values of the Boolean expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By selecting a.id and the desired frequency count (named CNT) from each group (defined by a.id) you get one observation per group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, you need the HAVING clause (and not a WHERE clause) to specify a selection criterion that involves the summary function SUM (through CNT).&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 15:56:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/254971#M48675</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-03-07T15:56:10Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255004#M48690</link>
      <description>You read me wrong. &lt;BR /&gt;Having is to filter with aggregate values (such as count ()).&lt;BR /&gt;But I understood your requirement that wished to filter on a value calculated on row level. Then count/having is inappropriate.</description>
      <pubDate>Mon, 07 Mar 2016 16:55:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255004#M48690</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-03-07T16:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255064#M48705</link>
      <description>&lt;P&gt;Hi Rw9,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, I need to compute the difference between all the rows of the &amp;nbsp;data set&amp;nbsp;a and &amp;nbsp;dataset b. In order to do so I use a join to do so, I 'll have &amp;nbsp;a&amp;nbsp;a&amp;nbsp;table of 4*4 =16 rows. I compute the difference between var1 and var2 .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I wanted &amp;nbsp;is to run a query that returns me counts of observations for which the difference is gt than 10. &amp;nbsp;In my example, &amp;nbsp;we know that there are 4 observetion for which the difference between var1 and var2 is gt 10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the solution provided by&amp;nbsp;&lt;SPAN&gt;FreelanceReinhard works fine :&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table case2 as
select a.id, sum(var1-var2 gt 10) as cnt
from a inner join b
on a.id=b.id
group by a.id
having cnt gt 3;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;saskap&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 19:10:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255064#M48705</guid>
      <dc:creator>saskapa</dc:creator>
      <dc:date>2016-03-07T19:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL having statement with count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255067#M48706</link>
      <description>&lt;P&gt;Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh﻿&lt;/a&gt;&lt;SPAN&gt;!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It does exactly what I want.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Note that the following method seems to work as well :&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&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 case2  as
 select  a.id
 from a as a inner join b as b
 on a.id=b.id
 where var1-var2  gt 10
 group by a.id
 having( count(*) gt  3 );
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I use a where clause, which select only the match that meets the conditin var1-var2 &amp;gt;10.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;saskap&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2016 19:27:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-having-statement-with-count/m-p/255067#M48706</guid>
      <dc:creator>saskapa</dc:creator>
      <dc:date>2016-03-07T19:27:09Z</dc:date>
    </item>
  </channel>
</rss>

