<?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 question - Count Distinct, Sum, Group by problems in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339660#M272620</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've solved this for myself now. &amp;nbsp;I had an extra proc sort de-dupe missing originally so I've added that in now and got the results I wanted. &amp;nbsp;Schoolboy error :-).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for all your help and comments, though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Mar 2017 15:15:12 GMT</pubDate>
    <dc:creator>BlueBoyDan</dc:creator>
    <dc:date>2017-03-09T15:15:12Z</dc:date>
    <item>
      <title>PROC SQL question - Count Distinct, Sum, Group by problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339590#M272616</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hopefully an easy question to solve, but I'm back to using Base SAS after 8 years of using SAS Enterprise Guide and I'm getting a mental block!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is my proc sql code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table mktg_count&lt;BR /&gt;as&lt;BR /&gt;select PROD_DETAIL,&lt;BR /&gt;count(distinct DM_CUST_KEY) as Total_Customers,&lt;BR /&gt;sum( case when valid_email = 'Y' and SUPRS_EMAIL_FLAG = 'N' and OPT_OUT_FLAG = 'N' then 1 else 0 end) as Emailable,&lt;BR /&gt;sum( case when SUPRS_MAIL_FLAG = 'N' and OPT_OUT_FLAG = 'N' then 1 else 0 end) as Mailable&lt;BR /&gt;from cust_emails&lt;BR /&gt;where PROD_DETAIL not in ('Home','Motor')&lt;BR /&gt;group by PROD_DETAIL;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm trying to do is create the Emailable and Mailable columns for the distinct values of Total Customers, but my sum function is bringing back the sum for the whole of my table instead.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anybody please help me with this so that the sum function is only on distinct customers. &amp;nbsp;I've tried putting the calculated columns in the group by statement, but that doesn't seem to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 11:45:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339590#M272616</guid>
      <dc:creator>BlueBoyDan</dc:creator>
      <dc:date>2017-03-09T11:45:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL question - Count Distinct, Sum, Group by problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339594#M272617</link>
      <description>&lt;P&gt;If you provide some example data to play around with (&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;in a data step&lt;/A&gt;), I could try to write some code for this.&lt;/P&gt;
&lt;P&gt;Basically I would sum up first on DM_CUST_KEY (I guess that if a DM_CUST_KEY has at least one valid_email = 'Y', you want to count that as 1 for that particular DM_CUST_KEY), and then sum on the PROD_DETAIL in a second step. But to get a feel for your data, I'd need that first.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 11:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339594#M272617</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-03-09T11:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL question - Count Distinct, Sum, Group by problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339596#M272618</link>
      <description>&lt;P&gt;Can you supply some test data - in the form of a datastep - and what the output should look like. &amp;nbsp;It is confusing to me that you have count(distinct DM_CUST_KEY), but want this to be unique keys? &amp;nbsp;Its either a count of that, or a unique list of but not both. &amp;nbsp;The where should be moved to a subquery or having clause. &amp;nbsp;And its likely a simple datastep or proc would do this better anyways. &amp;nbsp;But without some data its hard to say.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 12:04:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339596#M272618</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-09T12:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL question - Count Distinct, Sum, Group by problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339647#M272619</link>
      <description>&lt;P&gt;Not sure what you mean by unique customer in the sum functions. You have a nested case using three columns. How would you expect&amp;nbsp;that to be evaluated across multiple rows for a single customer?&lt;/P&gt;
&lt;P&gt;Perhaps it's easier to do a preparation&amp;nbsp;step to first give you unique rows for a customer, then do the count?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 14:38:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339647#M272619</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-03-09T14:38:51Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL question - Count Distinct, Sum, Group by problems</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339660#M272620</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've solved this for myself now. &amp;nbsp;I had an extra proc sort de-dupe missing originally so I've added that in now and got the results I wanted. &amp;nbsp;Schoolboy error :-).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for all your help and comments, though.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 15:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-question-Count-Distinct-Sum-Group-by-problems/m-p/339660#M272620</guid>
      <dc:creator>BlueBoyDan</dc:creator>
      <dc:date>2017-03-09T15:15:12Z</dc:date>
    </item>
  </channel>
</rss>

