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
SAS Super FREQ

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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