BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LMSSAS
Quartz | Level 8

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?

Lisa_Sessions_0-1643400092552.png

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Sounds like you just want to add that as one of the GROUP BY variables.

create table Summary_Combine_NS as
select 
   WorkType
 , Next_Step
 , count(*) as N_observations
  from CPIM_NS_Automation
  group by WorkType, Next_Step
;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

What is your data(sample) and the required output?

LMSSAS
Quartz | Level 8

I'm not sure if this is what you asking for. Its the original code with the output. Thank you!

 

Lisa_Sessions_0-1643400624523.png

PROC SQL;
   CONNECT TO DB2 AS DB1 (DATAsrc=IDS1P1 AUTHDOMAIN="DEFAULTAUTH");
CREATE TABLE CPIM_OPEN_INVENTORY_&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 <> 'CL'
/*  AND TMP2.VAL_DE <> 'Argentinian'*/ /* do not uncomment this filters out some o  the subworktypes that are needed*/
/*  AND TMP2.VAL_DE <> '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;
LMSSAS
Quartz | Level 8

 

Next_Step Contact New Contact Maint Agency New Agency Maint
Automation  Count  Count  Count  Count 
Manaul  Count  Count  Count  Count 
Blank  Count  Count  Count  Count 
Tom
Super User Tom
Super User

Read your SAS log.  It will show that SAS had to remerge the COUNT() aggregate value onto the detailed rows.

If you only want one observation per WORK_TYPE do not include NEXT_STEP in the list of variables to select.

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
;

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.

LMSSAS
Quartz | Level 8

Thank you, Tom 

 

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. 

Is that possible? 

Lisa_Sessions_0-1643635197997.png

 

Tom
Super User Tom
Super User

Sounds like you just want to add that as one of the GROUP BY variables.

create table Summary_Combine_NS as
select 
   WorkType
 , Next_Step
 , count(*) as N_observations
  from CPIM_NS_Automation
  group by WorkType, Next_Step
;
LMSSAS
Quartz | Level 8
Yes, that worked and gave me what I'm looking for..
Thank you!!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1114 views
  • 0 likes
  • 3 in conversation