<?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 count distinct Question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793465#M254346</link>
    <description>&lt;P&gt;Thank you, Tom&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used your query and it definitely got me closer in format to what I'm looking for. My ouput is below. The N_observations colum is actually counting the number of each Work type in the ouput data. What I need to count is the Number of Automation in the Next_Step column and the mumber of Manual in the Next-Step Column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that possible?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lisa_Sessions_0-1643635197997.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67984iC3E325A16F62DF2F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Lisa_Sessions_0-1643635197997.png" alt="Lisa_Sessions_0-1643635197997.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 31 Jan 2022 13:24:10 GMT</pubDate>
    <dc:creator>LMSSAS</dc:creator>
    <dc:date>2022-01-31T13:24:10Z</dc:date>
    <item>
      <title>Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793208#M254195</link>
      <description>&lt;P&gt;I am using proc sql to do a summary count of next-step automation and next-step manual, by Worktype: Contact New, Contact Maint, Agency New, and Agency Maint. There are a total of 4 WorkTypes. I am not sure how to structure the query for this. I have started with whats below and getting the output shown, clearly it isn’t right. Does anyone provide s suggestion?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lisa_Sessions_0-1643400092552.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67960iF6BBFEE530452B8B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Lisa_Sessions_0-1643400092552.png" alt="Lisa_Sessions_0-1643400092552.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
create table Summary_Combine_NS as
Select 
	'WorkType'n,
	'Next_Step'n,
	 count(Distinct 'Next_Step'n) as Counts
From CPIM_NS_Automation
Group by 'WorkType'n
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 20:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793208#M254195</guid>
      <dc:creator>LMSSAS</dc:creator>
      <dc:date>2022-01-28T20:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793209#M254196</link>
      <description>&lt;P&gt;What is your data(sample) and the required output?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 20:06:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793209#M254196</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2022-01-28T20:06:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793213#M254198</link>
      <description>&lt;P&gt;I'm not sure if this is what you asking for. Its the original code with the output. Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lisa_Sessions_0-1643400624523.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67961iC98A3051058BE960/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Lisa_Sessions_0-1643400624523.png" alt="Lisa_Sessions_0-1643400624523.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CONNECT TO DB2 AS DB1 (DATAsrc=IDS1P1 AUTHDOMAIN="DEFAULTAUTH");
CREATE TABLE CPIM_OPEN_INVENTORY_&amp;amp;SYSDATE._1 AS 
SELECT *
      FROM CONNECTION TO DB1
         (SELECT DISTINCT
			CPC.CHNL_PTNR_CASE_ID "TrackingCase"
			,CPT2.CHNL_PTNR_CASE_TYP_DE "WorkType"
			,CPT1.CHNL_PTNR_CASE_TYP_DE "SubWorkType"
			,CPC.CHNL_PTNR_CASE_BGN_DT "CreateDate"
			,TMP1.VAL_DE "Status"
			,TMP2.VAL_DE "SubStatus"
			,CPCS.SRC_SYS_CD "SourceCode"
			,AGET.CHNL_PTNR_CASE_SBJ_VAL_ID AS "AgentType"
			,AGEY.CHNL_PTNR_CASE_SBJ_VAL_ID AS "AgencyType"
			,CPC.CHNL_PTNR_DE AS 'Next_Step'n


FROM
CPM1P1.CHNL_PTNR_CASE CPC
LEFT JOIN CPM1P1.CHNL_PTNR_CASE_STEP CPCS ON CPC.CHNL_PTNR_CASE_ID = CPCS.CHNL_PTNR_CASE_ID  
LEFT JOIN CPM1P1.CHNL_PTNR_CASE_TYP CPT1 ON CPC.CHNL_PTNR_CASE_TYP_CD = CPT1.CHNL_PTNR_CASE_TYP_CD
LEFT JOIN CPM1P1.CHNL_PTNR_CASE_TYP CPT2 ON CPT1.PRNT_CHNL_PTNR_CASE_TYP_CD = CPT2.CHNL_PTNR_CASE_TYP_CD
LEFT JOIN CPM1P1.CHNL_PTNR_CASE_PTY CPCP ON UPPER(CPCS.RESP_PTY_ID) = UPPER(CPCP.CHNL_PTNR_CASE_PTY_ID)
LEFT JOIN CPM1P1.TMP_CD_VAL TMP1 ON CPCS.CHNL_PTNR_CASE_STEP_STAT_CD = TMP1.VAL_CD
LEFT JOIN CPM1P1.TMP_CD_VAL TMP2 ON CPCS.CASE_STEP_SUB_STAT_CD = TMP2.VAL_CD

INNER JOIN (
		SELECT DISTINCT CPCS2.CHNL_PTNR_CASE_ID, 
			MAX(CPCS2.CHNL_PTNR_CASE_STEP_NB) NMBR
            FROM CPM1P1.CHNL_PTNR_CASE_STEP CPCS2
            GROUP BY CHNL_PTNR_CASE_ID) SUB
            ON CPCS.CHNL_PTNR_CASE_ID = SUB.CHNL_PTNR_CASE_ID 
			AND CPCS.CHNL_PTNR_CASE_STEP_NB = SUB.NMBR
            
LEFT JOIN (SELECT DISTINCT
          SUB3.CHNL_PTNR_CASE_ID, SUB3.CHNL_PTNR_CASE_SBJ_VAL_ID
          FROM CPM1P1.CHNL_PTNR_CASE_SBJ SUB3
          WHERE SUB3.CHNL_PTNR_CASE_SBJ_TYP_CD = 'AGENCYTYPE') AGEY
          ON CPCS.CHNL_PTNR_CASE_ID = AGEY.CHNL_PTNR_CASE_ID
          
LEFT JOIN (SELECT DISTINCT
          SUB4.CHNL_PTNR_CASE_ID, SUB4.CHNL_PTNR_CASE_SBJ_VAL_ID
          FROM CPM1P1.CHNL_PTNR_CASE_SBJ SUB4
          WHERE SUB4.CHNL_PTNR_CASE_SBJ_TYP_CD = 'CONTACTAGENTTYPE') AGET
          ON CPCS.CHNL_PTNR_CASE_ID = AGET.CHNL_PTNR_CASE_ID
            
WHERE
  CPCS.CHNL_PTNR_CASE_STEP_STAT_CD &amp;lt;&amp;gt; 'CL'
/*  AND TMP2.VAL_DE &amp;lt;&amp;gt; 'Argentinian'*/ /* do not uncomment this filters out some o  the subworktypes that are needed*/
/*  AND TMP2.VAL_DE &amp;lt;&amp;gt; 'Arkansas'*/ /* do not uncomment this filters out some of the subworktypes that are needed*/
ORDER BY CPC.CHNL_PTNR_CASE_ID ASC);
DISCONNECT FROM DB1;  
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Jan 2022 20:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793213#M254198</guid>
      <dc:creator>LMSSAS</dc:creator>
      <dc:date>2022-01-28T20:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793219#M254200</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="440"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="84"&gt;Next_Step&lt;/TD&gt;
&lt;TD width="86"&gt;Contact New&lt;/TD&gt;
&lt;TD width="94"&gt;Contact Maint&lt;/TD&gt;
&lt;TD width="84"&gt;Agency New&lt;/TD&gt;
&lt;TD width="92"&gt;Agency Maint&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Automation&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Manaul&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Blank&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;Count&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Fri, 28 Jan 2022 20:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793219#M254200</guid>
      <dc:creator>LMSSAS</dc:creator>
      <dc:date>2022-01-28T20:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793237#M254210</link>
      <description>&lt;P&gt;Read your SAS log.&amp;nbsp; It will show that SAS had to remerge the COUNT() aggregate value onto the detailed rows.&lt;/P&gt;
&lt;P&gt;If you only want one observation per WORK_TYPE do not include NEXT_STEP in the list of variables to select.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table Summary_Combine_NS as
select 
  'WorkType'n
 , count(distinct 'Next_Step'n) as Distinct_Steps
 , count(*) as N_observations
  from CPIM_NS_Automation
  group by 'WorkType'n
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that most other implementations of SQL will not allow a query using GROUP BY where there are variables being selected that are neither GROUP BY variables nor aggregate function results.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 22:50:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793237#M254210</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-28T22:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793465#M254346</link>
      <description>&lt;P&gt;Thank you, Tom&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used your query and it definitely got me closer in format to what I'm looking for. My ouput is below. The N_observations colum is actually counting the number of each Work type in the ouput data. What I need to count is the Number of Automation in the Next_Step column and the mumber of Manual in the Next-Step Column.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that possible?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lisa_Sessions_0-1643635197997.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67984iC3E325A16F62DF2F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Lisa_Sessions_0-1643635197997.png" alt="Lisa_Sessions_0-1643635197997.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Jan 2022 13:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793465#M254346</guid>
      <dc:creator>LMSSAS</dc:creator>
      <dc:date>2022-01-31T13:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793466#M254347</link>
      <description>&lt;P&gt;Sounds like you just want to add that as one of the GROUP BY variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;create table Summary_Combine_NS as
select 
   WorkType
 , Next_Step
 , count(*) as N_observations
  from CPIM_NS_Automation
  group by WorkType, Next_Step
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 Jan 2022 13:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793466#M254347</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-31T13:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL count distinct Question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793467#M254348</link>
      <description>Yes, that worked and gave me what I'm looking for..&lt;BR /&gt;Thank you!!</description>
      <pubDate>Mon, 31 Jan 2022 13:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-count-distinct-Question/m-p/793467#M254348</guid>
      <dc:creator>LMSSAS</dc:creator>
      <dc:date>2022-01-31T13:45:01Z</dc:date>
    </item>
  </channel>
</rss>

