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

Hi,

Would anyone know how to, by using PROC SQL merge duplicate variables rows and this would result in a sum up of one column?

tried SELECT DISTINCT and GROUP BY but I am still receiving multiple duplicates. 

 

Get duplicate rows showing for each Product #... don't want to use  PROC SORT nodupkey -> it will remove all but 1 and the amount for Jan will not be correct... 

 

I want the Product #  and code to be distinct  - if there are amounts in both months then it should be one line.

 

 

please HELP  - see code below

 

proc sql; 
create table TRANSPOSE_VIEW as 

select distinct
Product,    
ADJ_REASON_CODE,    
CODE,    
ADJ_ACTIVITY1_CATG,   
gl_DESC1,   
ADJ_ACTIVITY2_CATG,
case repMTD_CD when 'CM' then repMTH end as CM,
case repMTD_CD when 'CM' then SAP_AMOUNT end as JAN_AMOUNT,
case repMTD_CD when 'PM' then repMTH end as PM,
case repMTD_CD when 'PM' then SAP_AMOUNT end as FRB_AMOUNT
 

from CATSegmentRepAll

group by
Product, 
ADJ_REASON_CODE, 
CODE,
ADJ_ACTIVITY1_CATG,
ADJ_ACTIVITY2_CATG
;QUIT;

this is what I am getting 

Product #ADJ_REASON_CODECODEADJ_ACTIVITY1_CATGgl_DESC1ADJ_ACTIVITY2_CATGCMAPRIL_AMOUNTPMMARCH_AMOUNT
50000683DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50000683DISCOASBACT60PricingContraActivation/Port-in Credits2018-0460  
50001288DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50002333DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50002333DISCOASBACT60PricingContraActivation/Port-in Credits2018-0460  
50003357DISCOASBACT50PricingContraActivation/Port-in Credits  2018-0350
50004676DISCOASBACT70PricingContraActivation/Port-in Credits  2018-0370
50004676DISCOASBACT70PricingContraActivation/Port-in Credits2018-0470  
50005846DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50005846DISCOASBACT60PricingContraActivation/Port-in Credits2018-0460  
50006022DISCOASBACT50PricingContraActivation/Port-in Credits  2018-0350
50006022DISCOASBACT50PricingContraActivation/Port-in Credits2018-0450  
50006756DISCOASBACT10PricingContraActivation/Port-in Credits  2018-0310
50006756DISCOASBACT10PricingContraActivation/Port-in Credits2018-0410  

 

 

but what I want is 

 

Product #ADJ_REASON_CODECODEADJ_ACTIVITY1_CATGgl_DESC1ADJ_ACTIVITY2_CATGCMAPRIL_AMOUNTPMMARCH_AMOUNT
50000683DISCOASBACT60PricingContraActivation/Port-in Credits2018-04602018-0360
50001288DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50002333DISCOASBACT60PricingContraActivation/Port-in Credits2018-04602018-0360
50003357DISCOASBACT50PricingContraActivation/Port-in Credits  2018-0350
50004676DISCOASBACT70PricingContraActivation/Port-in Credits2018-04702018-0370
50005846DISCOASBACT60PricingContraActivation/Port-in Credits2018-04602018-0360
50006022DISCOASBACT50PricingContraActivation/Port-in Credits2018-04502018-0350
50006756DISCOASBACT10PricingContraActivation/Port-in Credits2018-04102018-0310
1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Make sure that all your select list columns, except the aggregated columns, are mentioned in the GROUP BY list. Use the CALCULATED keyword to refer to new columns. Follow this structure:

 

proc sql;
create table want as
select 
    x, 
    y, 
    z, 
    r+u as newVar,
    sum(t) as sumVar
from have
group by x, y, z, calculated newVar;
quit;
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

Make sure that all your select list columns, except the aggregated columns, are mentioned in the GROUP BY list. Use the CALCULATED keyword to refer to new columns. Follow this structure:

 

proc sql;
create table want as
select 
    x, 
    y, 
    z, 
    r+u as newVar,
    sum(t) as sumVar
from have
group by x, y, z, calculated newVar;
quit;
PG
sufiya
Quartz | Level 8

 

Hi @PGStats, I updated the 'group by" in the code - see below. still getting the same duplicates. 

 

Product #ADJ_REASON_CODECODEADJ_ACTIVITY1_CATGgl_DESC1ADJ_ACTIVITY2_CATGCMAPRIL_AMOUNTPMMARCH_AMOUNT
50000683DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50000683DISCOASBACT60PricingContraActivation/Port-in Credits2018-0460  
50001288DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50002333DISCOASBACT60PricingContraActivation/Port-in Credits  2018-0360
50002333DISCOASBACT60PricingContraActivation/Port-in Credits2018-0460  

 

proc sql; create table SegmentALL as 
select distinct
SUBSCRIBER_NO,    
ADJ_REASON_CODE,    
DISCOUNT_CODE,    
ADJ_ACTIVITY1_CATG,   
gl_DESC1,  
segment,   
ADJ_ACTIVITY2_CATG,
case repMTD_CD when 'CM' then repMTH end as CM,
case repMTD_CD when 'CM' then SAP_AMOUNT end as APRIL_AMOUNT,
case repMTD_CD when 'CM' then Instances end as APRIL_Transactions,
case repMTD_CD when 'PM' then repMTH end as PM,
case repMTD_CD when 'PM' then SAP_AMOUNT end as MARCH_AMOUNT,
case repMTD_CD when 'PM' then Instances end as MARCH_Transactions 

from CATSegmentRepAll


group by
SUBSCRIBER_NO,    
ADJ_REASON_CODE,    
DISCOUNT_CODE,    
ADJ_ACTIVITY1_CATG,   
gl_DESC1,  
segment,   
ADJ_ACTIVITY2_CATG
;QUIT;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2 replies
  • 869 views
  • 2 likes
  • 2 in conversation