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

Good day everyone ! 

Currently I'm solving one complicated task and got stuck.

Better explain on example:

I have:

IDCustomerDatePRODSUMDigitalRandom field
ID1C105.05.2021P1151AAA
ID2C105.05.2021P1160AAA
ID3C206.05.2021P2211BBB

 

Given this data set I need to collapse rows when Customer, Date and Prod are equal. In fact, it is one deal, written on a different rows. Field SUM I need to sum up when collapsing, Digital - need to take average of rows. Random Field rows are identical in a group of deals. 

 

As an output:

CustomerDatePRODSUMDigitalRandom fieldCount rows
C105.05.2021P1310.5AAA2
C206.05.2021P2211BBB1

   

Please help, this is really complicated to me 😃 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

Not sure if your date is DDMMYY or MMDDYY. Here in Europe it's DDMMYY.

data have;
input 
ID $ Customer $ Date : ddmmyy10. PROD $ SUM Digital Random_field $;
format Date date9.;
cards;
ID1	C1	05.05.2021	P1	15	1	AAA
ID2	C1	05.05.2021	P1	16	0	AAA
ID3	C2	06.05.2021	P2	21	1	BBB
;
run;

PROC MEANS data=have(drop=ID) SUM MEAN nway noprint;
 CLASS Customer Date PROD;
 var SUM Digital;
 output out=want(drop=_TYPE_ SUM_mean Digital_sum) sum= mean= / autoname;
run;

proc print; run;
/* end of program */

Regards,

Koen

View solution in original post

3 REPLIES 3
Banker337
Fluorite | Level 6

Well, I figured it out)

 

proc sql noprint; 
  create table output as 
    select Customer, Date, Prod, 
		Randomfield
      ,sum(Sum) as Sum 
      ,avg(Digital) as Digital
      ,count(*) as count
  from work.data_test
  group by Customer, Date, Prod 
;quit;
sbxkoenk
SAS Super FREQ

Not sure if your date is DDMMYY or MMDDYY. Here in Europe it's DDMMYY.

data have;
input 
ID $ Customer $ Date : ddmmyy10. PROD $ SUM Digital Random_field $;
format Date date9.;
cards;
ID1	C1	05.05.2021	P1	15	1	AAA
ID2	C1	05.05.2021	P1	16	0	AAA
ID3	C2	06.05.2021	P2	21	1	BBB
;
run;

PROC MEANS data=have(drop=ID) SUM MEAN nway noprint;
 CLASS Customer Date PROD;
 var SUM Digital;
 output out=want(drop=_TYPE_ SUM_mean Digital_sum) sum= mean= / autoname;
run;

proc print; run;
/* end of program */

Regards,

Koen

Cynthia_sas
Diamond | Level 26

Hi:

  And to add to the list of possible solutions, here's a PROC REPORT solution:

Cynthia_sas_0-1628012048427.png

And, TABULATE would do it too, although the arrangement of variables needs to change a bit for TABULATE to do the collapsing and summarizing:

Cynthia_sas_1-1628012281232.png

Hope this gives you more ideas.

Cynthia

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 870 views
  • 2 likes
  • 3 in conversation