<?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 proc sql sql count using case when group by inside count() in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773688#M245812</link>
    <description>&lt;P&gt;Hi, I wanted to count events on dates using count() in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to:&lt;/P&gt;
&lt;P&gt;1. Count the first date as OPEN if after certain date&lt;/P&gt;
&lt;P&gt;2. Count the last date as CLOSED if before certain date&lt;/P&gt;
&lt;P&gt;3. Count the number of visits between OPEN and close&lt;/P&gt;
&lt;P&gt;All using proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have0;
 infile cards truncover expandtabs;
 input STORE_ID $ CUSTOMER_ID DATE :date9. ;
 format date date8. ;
 cards;
A0001 7001 12Oct2015 
A0001 7002 12Oct2015 
A0001 7001 12Nov2015 
A0001 7001 15Nov2016 
A0001 7002 13Oct2016 
A0001 7003 13Oct2015 

A0002 7001 12Oct2015 
A0002 7002 12Oct2015 
A0002 7003 12Oct2015 
A0002 7001 20Oct2016 
A0002 7002 23Oct2016 
A0002 7003 30Nov2016 

;
run;
proc sql;
    create table test as
    select distinct store_id,
    max(date) as close_date,
    min(date) as open_date,
    count(distinct case when min(date) ge '12-OCT-2016'd and max(date) le '30-NOV-2019'd) then ) as  /*not sure syntax here*/
    count(distinct customer_id) as no_customers  /*not sure if this should be based on previous line?*/
    group by store_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not sure how to format the count distinct case to capture all transactions between the open and close dates.&lt;/P&gt;
&lt;P&gt;Similarly, I want to count the number of distinct customers between those dates too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="A_Swoosh_0-1634059310647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64618i82C291DED5F1F33F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="A_Swoosh_0-1634059310647.png" alt="A_Swoosh_0-1634059310647.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated!&lt;/P&gt;</description>
    <pubDate>Tue, 12 Oct 2021 17:22:04 GMT</pubDate>
    <dc:creator>A_Swoosh</dc:creator>
    <dc:date>2021-10-12T17:22:04Z</dc:date>
    <item>
      <title>proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773688#M245812</link>
      <description>&lt;P&gt;Hi, I wanted to count events on dates using count() in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to:&lt;/P&gt;
&lt;P&gt;1. Count the first date as OPEN if after certain date&lt;/P&gt;
&lt;P&gt;2. Count the last date as CLOSED if before certain date&lt;/P&gt;
&lt;P&gt;3. Count the number of visits between OPEN and close&lt;/P&gt;
&lt;P&gt;All using proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have0;
 infile cards truncover expandtabs;
 input STORE_ID $ CUSTOMER_ID DATE :date9. ;
 format date date8. ;
 cards;
A0001 7001 12Oct2015 
A0001 7002 12Oct2015 
A0001 7001 12Nov2015 
A0001 7001 15Nov2016 
A0001 7002 13Oct2016 
A0001 7003 13Oct2015 

A0002 7001 12Oct2015 
A0002 7002 12Oct2015 
A0002 7003 12Oct2015 
A0002 7001 20Oct2016 
A0002 7002 23Oct2016 
A0002 7003 30Nov2016 

;
run;
proc sql;
    create table test as
    select distinct store_id,
    max(date) as close_date,
    min(date) as open_date,
    count(distinct case when min(date) ge '12-OCT-2016'd and max(date) le '30-NOV-2019'd) then ) as  /*not sure syntax here*/
    count(distinct customer_id) as no_customers  /*not sure if this should be based on previous line?*/
    group by store_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not sure how to format the count distinct case to capture all transactions between the open and close dates.&lt;/P&gt;
&lt;P&gt;Similarly, I want to count the number of distinct customers between those dates too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="A_Swoosh_0-1634059310647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64618i82C291DED5F1F33F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="A_Swoosh_0-1634059310647.png" alt="A_Swoosh_0-1634059310647.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 17:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773688#M245812</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-10-12T17:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773689#M245813</link>
      <description>&lt;P&gt;Could you also post a `want` data set containing what you want the data to ultimately look like? Just take your `have` DATA step and reduce it down to what you want. I've also formatted your code to make it more readable for others.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;    
	create table 	test as
		select 
					distinct store_id,
					max(date) as close_date,
					min(date) as open_date, 
					count(distinct case when min(date) ge '12-OCT-2016'd and max(date) le '30-NOV-2019'd) then ) as  /*not sure syntax here*/ 
					count(distinct customer_id) as no_customers  /*not sure if this should be based on previous line?*/ 
		group by 
					store_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Oct 2021 17:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773689#M245813</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-10-12T17:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773692#M245814</link>
      <description>This is a case where a data step is much more efficient, is there a particular reason for SQL?</description>
      <pubDate>Tue, 12 Oct 2021 17:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773692#M245814</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-12T17:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773694#M245815</link>
      <description>Added a want dataset. And I thank you for formatting. On my screen it looks fine--strange.</description>
      <pubDate>Tue, 12 Oct 2021 17:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773694#M245815</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-10-12T17:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773695#M245816</link>
      <description>No particular reason actually--just what I thought would be ideal in this particular case.</description>
      <pubDate>Tue, 12 Oct 2021 17:23:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773695#M245816</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-10-12T17:23:46Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773696#M245817</link>
      <description>&lt;P&gt;This may be what you're looking for then. Your initial explanation was confusing to me.&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 store_ID, count(distinct customer_ID) as no_customers,
count(*) as n0_transactions
from have0
where date between '12OCT2016'd and '30Nov2019'd;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I wanted to count events on dates using count() in proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to:&lt;/P&gt;
&lt;P&gt;1. Count the first date as OPEN if after certain date&lt;/P&gt;
&lt;P&gt;2. Count the last date as CLOSED if before certain date&lt;/P&gt;
&lt;P&gt;3. Count the number of visits between OPEN and close&lt;/P&gt;
&lt;P&gt;All using proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have0;
 infile cards truncover expandtabs;
 input STORE_ID $ CUSTOMER_ID DATE :date9. ;
 format date date8. ;
 cards;
A0001 7001 12Oct2015 
A0001 7002 12Oct2015 
A0001 7001 12Nov2015 
A0001 7001 15Nov2016 
A0001 7002 13Oct2016 
A0001 7003 13Oct2015 

A0002 7001 12Oct2015 
A0002 7002 12Oct2015 
A0002 7003 12Oct2015 
A0002 7001 20Oct2016 
A0002 7002 23Oct2016 
A0002 7003 30Nov2016 

;
run;
proc sql;
    create table test as
    select distinct store_id,
    max(date) as close_date,
    min(date) as open_date,
    count(distinct case when min(date) ge '12-OCT-2016'd and max(date) le '30-NOV-2019'd) then ) as  /*not sure syntax here*/
    count(distinct customer_id) as no_customers  /*not sure if this should be based on previous line?*/
    group by store_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not sure how to format the count distinct case to capture all transactions between the open and close dates.&lt;/P&gt;
&lt;P&gt;Similarly, I want to count the number of distinct customers between those dates too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="A_Swoosh_0-1634059310647.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64618i82C291DED5F1F33F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="A_Swoosh_0-1634059310647.png" alt="A_Swoosh_0-1634059310647.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help would be appreciated!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Oct 2021 17:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773696#M245817</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-12T17:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773697#M245818</link>
      <description>Thank you for the quick reply. This was just a simplified case I had. Open and close dates are going to vary based on store so using predefined dates would not always work which is why I was trying to create a case scenario based on the min and max dates.</description>
      <pubDate>Tue, 12 Oct 2021 17:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773697#M245818</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-10-12T17:34:18Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql sql count using case when group by inside count()</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773704#M245824</link>
      <description>Well....I can only answer the question as posted ... &lt;BR /&gt;&lt;BR /&gt;I would instead then create a second table with each store ID and the start/end date. Then you can modify the query posted by joining the have0 table to the lookup table with the dates on Store ID and change the dates in the BETWEEN operator to be the variables from the look up table.</description>
      <pubDate>Tue, 12 Oct 2021 17:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-sql-count-using-case-when-group-by-inside-count/m-p/773704#M245824</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-10-12T17:49:55Z</dc:date>
    </item>
  </channel>
</rss>

