<?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: sql count using case when group by inside count() in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624906#M184141</link>
    <description>Correction: I meant "The results for events_cnt should be 4 because I want to count the total distinct dates for each ID."</description>
    <pubDate>Fri, 14 Feb 2020 17:43:32 GMT</pubDate>
    <dc:creator>leehsin</dc:creator>
    <dc:date>2020-02-14T17:43:32Z</dc:date>
    <item>
      <title>sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624905#M184140</link>
      <description>&lt;P&gt;Hi, I wanted to count events on dates using count() in proc sql. Here is the sample code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have0;
 infile cards truncover expandtabs;
 input ID $ CODE DATE :date9. DEPT $ ;
 format date date8. ;
 cards;
A0001 7001 12Oct2015 ETA 
A0001 7002 12Oct2015 ETA 
A0001 7001 12Nov2015 ETA 
A0001 7001 15Nov2016 ETA 
A0001 7002 13Oct2016 ETB 
A0001 7003 13Oct2015 ETB 

A0002 7001 12Oct2015 ETA  
A0002 7002 12Oct2015 ETA  
A0002 7003 12Oct2015 ETA  
A0002 7001 20Oct2016 ETB  
A0002 7002 23Oct2016 ETB  
A0002 7003 30Nov2016 ETB  

;
run;

PROC SQL;
   CREATE TABLE events_ct AS
   SELECT 
		count(*) as line_cnt
        ,count(distinct ID) as ID_cnt
		,count(distinct CODE) as CODE_cnt
		,count(distinct case when DEPT="ETA" then CODE end) as EtA_CODE_cnt
		,count(case when DEPT="ETA" then DATE end) as events_cnt
      FROM have0
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The results for events_cnt shoud be 3 because I want to count the total distinct dates for each ID.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need actually is something like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(distinct case when DEPT="ETA" then (DATE from have0 group by ID) end) as events_cnt&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but this code doesn't work as it is now. It there any solution to modify the &lt;CODE class=" language-sas"&gt;count(case when DEPT="ETA" then DATE end) as events_cnt&amp;nbsp;and get what I need? &lt;/CODE&gt;Thanks a lot !&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 17:38:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624905#M184140</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-14T17:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624906#M184141</link>
      <description>Correction: I meant "The results for events_cnt should be 4 because I want to count the total distinct dates for each ID."</description>
      <pubDate>Fri, 14 Feb 2020 17:43:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624906#M184141</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-14T17:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624909#M184143</link>
      <description>Add a GROUP BY clause to tell SAS to process it by specific groups. &lt;BR /&gt;&lt;BR /&gt;GROUP BY ID  is what you need.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 14 Feb 2020 17:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624909#M184143</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-14T17:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624912#M184144</link>
      <description>Where do I put GROUP BY ID? I don't want to put it after FROM Have0 as this will affect other COUNT statements.</description>
      <pubDate>Fri, 14 Feb 2020 17:52:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624912#M184144</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-14T17:52:53Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624925#M184152</link>
      <description>In that case that won't work and you need either a subquery or do multiple queries and combine the results. I would probably recommending the second option - do your queries separately and merge them for clarity. It's easier to manage your analysis in this fashion.</description>
      <pubDate>Fri, 14 Feb 2020 18:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624925#M184152</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-14T18:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624931#M184155</link>
      <description>&lt;P&gt;Please provide an example showing all the different counts that you want to perform, including expected results.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 19:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624931#M184155</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-02-14T19:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624968#M184169</link>
      <description>&lt;P&gt;These are the 4 records I want to count:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;A0001 12Oct2015 ETA&lt;BR /&gt;A0001 12Nov2015 ETA&lt;BR /&gt;A0001 15Nov2016 ETA&lt;BR /&gt;A0002 12Oct2015 ETA&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2020 23:00:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624968#M184169</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-14T23:00:27Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624974#M184172</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215142"&gt;@leehsin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;These are the 4 records I want to count:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;A0001 12Oct2015 ETA&lt;BR /&gt;A0001 12Nov2015 ETA&lt;BR /&gt;A0001 15Nov2016 ETA&lt;BR /&gt;A0002 12Oct2015 ETA&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That does NOT look like counts.&lt;/P&gt;
&lt;P&gt;What are the counts you want for the 12 observations in your example dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Feb 2020 01:32:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624974#M184172</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-15T01:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624975#M184173</link>
      <description>&lt;P&gt;Are you looking for something like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select id
     , count(*) as line_cnt
     , count(distinct code) as code_cnt
     , count(distinct date) as date_cnt
     , count(distinct dept) as dept_cnt
     , count(case when dept='ETA' then 1 end) as eta_cnt
     , count(distinct case when dept='ETA' then code end) as eta_code_cnt
     , count(distinct case when dept='ETA' then date end) as eta_date_cnt
     , count(distinct case when dept='ETA' then dept end) as eta_dept_cnt
from have
group by id
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                                                               eta_      eta_      eta_
Obs   ID    line_cnt  code_cnt  date_cnt  dept_cnt  eta_cnt  code_cnt  date_cnt  dept_cnt

 1   A0001      6         3         5         2        4         2         3        1
 2   A0002      6         3         4         2        3         3         1        1
&lt;/PRE&gt;</description>
      <pubDate>Sat, 15 Feb 2020 05:27:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624975#M184173</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-15T05:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624980#M184174</link>
      <description>&lt;P&gt;This counts what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  count(unique case when DEPT="ETA" then cats(DATE,CODE) end) -1 as EVENTS_CNT&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You need to substract 1 as an empty string will also be counted when DEPT &amp;lt;&amp;gt; 'ETA'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Feb 2020 02:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/624980#M184174</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-15T02:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/625279#M184285</link>
      <description>I meant these are the 4 records to be counted, ---- 3 different dates for A0001, and 1 different date for A0002.</description>
      <pubDate>Mon, 17 Feb 2020 14:36:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/625279#M184285</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-17T14:36:10Z</dc:date>
    </item>
    <item>
      <title>Re: sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/625283#M184288</link>
      <description>&lt;P&gt;Thank you, ChrisNZ! This is the solution I am looking for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I just change 'CODE' to 'ID' as I want to group the count of dates by ID.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count(unique case when DEPT="ETA" then cats(DATE,ID) end) as EVENTS_CNT&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The result to have:&lt;/P&gt;&lt;P&gt;lin_cnt&amp;nbsp; ID_cnt&amp;nbsp; CODE_cnt&amp;nbsp; EtA_CODE_cnt&amp;nbsp; envents_cnt&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&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;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/P&gt;</description>
      <pubDate>Mon, 17 Feb 2020 15:50:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sql-count-using-case-when-group-by-inside-count/m-p/625283#M184288</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2020-02-17T15:50:36Z</dc:date>
    </item>
  </channel>
</rss>

