<?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 Count Distinct nested within Sum Function based on Case When Statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884193#M349288</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I coded the following thinking it would work but unfortunately SAS apparently cannot do what I am trying to do. Is there another way to accomplish this table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.BUCKET,
				t1.GROUP,
				t1.CODE,
				t1.CODE_DESC,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN COUNT(DISTINCT(t1.UNIQ_ID)) ELSE 0 END) AS IN_UNIQ_ID,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_DOLLARS ELSE 0 END) AS IN_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_FIELD_COUNT ELSE 0 END) AS IN_FIELD_COUNT,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN (CALCULATED IN_DOLLARS/CALCULATED IN_FIELD_COUNT) ELSE 0 END) AS IN_AVG_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN COUNT(DISTINCT(t1.UNIQ_ID))ELSE 0 END) AS OUT_UNIQ_ID,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_DOLLARS ELSE 0 END) AS OUT_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_FIELD_COUNT ELSE 0 END) AS OUT_FIELD_COUNT,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN (CALCULATED OUT_DOLLARS/CALCULATED OUT_FIELD_COUNT) ELSE 0 END) AS OUT_AVG_DOLLARS,
		FROM WORK.HAVE t1
		GROUP BY 1,2,3,4
		ORDER BY t1.BUCKET,
				 t1.GROUP,
				 CALCULATED IN_FIELD_COUNT DESC;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the following errors:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Summary functions nested in this way are not supported.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help here is much appreciated! Thank you in advance!&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jul 2023 13:51:43 GMT</pubDate>
    <dc:creator>mhoward2</dc:creator>
    <dc:date>2023-07-10T13:51:43Z</dc:date>
    <item>
      <title>Count Distinct nested within Sum Function based on Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884193#M349288</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I coded the following thinking it would work but unfortunately SAS apparently cannot do what I am trying to do. Is there another way to accomplish this table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE WORK.WANT AS
		SELECT  t1.BUCKET,
				t1.GROUP,
				t1.CODE,
				t1.CODE_DESC,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN COUNT(DISTINCT(t1.UNIQ_ID)) ELSE 0 END) AS IN_UNIQ_ID,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_DOLLARS ELSE 0 END) AS IN_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_FIELD_COUNT ELSE 0 END) AS IN_FIELD_COUNT,
				SUM(CASE WHEN t1.INDICATOR='IN' THEN (CALCULATED IN_DOLLARS/CALCULATED IN_FIELD_COUNT) ELSE 0 END) AS IN_AVG_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN COUNT(DISTINCT(t1.UNIQ_ID))ELSE 0 END) AS OUT_UNIQ_ID,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_DOLLARS ELSE 0 END) AS OUT_DOLLARS,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_FIELD_COUNT ELSE 0 END) AS OUT_FIELD_COUNT,
				SUM(CASE WHEN t1.INDICATOR='OUT' THEN (CALCULATED OUT_DOLLARS/CALCULATED OUT_FIELD_COUNT) ELSE 0 END) AS OUT_AVG_DOLLARS,
		FROM WORK.HAVE t1
		GROUP BY 1,2,3,4
		ORDER BY t1.BUCKET,
				 t1.GROUP,
				 CALCULATED IN_FIELD_COUNT DESC;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get the following errors:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: Result of WHEN clause 2 is not the same data type as the preceding results.
ERROR: Summary functions nested in this way are not supported.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help here is much appreciated! Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2023 13:51:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884193#M349288</guid>
      <dc:creator>mhoward2</dc:creator>
      <dc:date>2023-07-10T13:51:43Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct nested within Sum Function based on Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884209#M349297</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279427"&gt;@mhoward2&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid the nesting of the summary functions SUM() and COUNT(DISTINCT ...) I suggest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;COUNT(DISTINCT CASE WHEN t1.INDICATOR='IN' THEN t1.UNIQ_ID ELSE ' ' END) AS IN_UNIQ_ID&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(and analogously for 'OUT').&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regarding the error message about inconsistent data types, make sure that the THEN and ELSE branch result in the same data type. The code above assumes that UNIQ_ID is a character variable, hence the character missing in the ELSE branch. If UNIQ_ID is numeric, then use &lt;FONT face="courier new,courier"&gt;ELSE . END&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To avoid the nesting of the SUM() functions (via a CALCULATED item) I suggest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;CALCULATED IN_DOLLARS/CALCULATED IN_FIELD_COUNT AS IN_AVG_DOLLARS&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(and analogously for 'OUT'). Please check if the results match your intentions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also remove the comma after the last SELECT item to avoid a syntax error.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2023 15:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884209#M349297</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-07-10T15:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Count Distinct nested within Sum Function based on Case When Statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884218#M349303</link>
      <description>&lt;P&gt;Does the source data have multiple observations per UNIQ_ID * INDICATION combination?&lt;/P&gt;
&lt;P&gt;If not then the problem a bit easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
CREATE TABLE WORK.WANT AS
  SELECT t1.BUCKET
       , t1.GROUP
       , t1.CODE
       , t1.CODE_DESC
       , SUM( t1.INDICATOR='IN' ) as IN_UNIQ_ID
       , SUM( CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_DOLLARS ELSE . END) AS IN_DOLLARS
       , MEAN( CASE WHEN t1.INDICATOR='IN' THEN t1.SUM_OF_DOLLARS ELSE . END) AS IN_AVG_DOLLARS
       , SUM( t1.INDICATOR='OUT' ) as OUT_UNIQ_ID
       , SUM( CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_DOLLARS ELSE . END) AS OUT_DOLLARS
       , MEAN( CASE WHEN t1.INDICATOR='OUT' THEN t1.SUM_OF_DOLLARS ELSE . END) AS OUT_AVG_DOLLARS
  FROM HAVE t1
  GROUP BY BUCKET
         , GROUP
         , IN_FIELD_COUNT DESC
;
QUIT;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS:&amp;nbsp; &amp;nbsp;Hiding separators at the end of the line, like in your original posting:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1689006044984.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85697i6A82DBABCEA49353/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1689006044984.png" alt="Tom_0-1689006044984.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Make it really difficult for humans to notice extra (or missing) separators:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1689006132354.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85698i0B6FD2488363F657/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_1-1689006132354.png" alt="Tom_1-1689006132354.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;It is much easier to just scan down the evenly positioned left side of the lines to check for the separators:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_2-1689006262275.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85699i33D2BD764BA6954C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_2-1689006262275.png" alt="Tom_2-1689006262275.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2023 16:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-Distinct-nested-within-Sum-Function-based-on-Case-When/m-p/884218#M349303</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-10T16:24:28Z</dc:date>
    </item>
  </channel>
</rss>

