BookmarkSubscribeRSS Feed
wagh
Calcite | Level 5

Hi, I have a summarized dataset which conatins 6 different products by their monthly sales. These 6 products belong to two different groups. ABC is group 1 and DEF belong to group2. It looks like this.

NameMonth1Month2Month3
A101010
B202040
C403020
D304060
E105050
F206030

I need to add rows to the data which sum up sales of all products in that group. It should look like this:

NameMonth1Month2Month3
A101010
B202040
C403020
SUM_ABC706070
D304060
E105050
F206030
SUM_DEF60150140

 

Could anyone please tell me how should it be done? Thank you.

2 REPLIES 2
r_behata
Barite | Level 11

Assumption made : Groups are pre-existing.

 

data have;
length name $10.;
input Grp $ Name	$ Month1	Month2	Month3;

cards;
1 A 10 10 10
1 B 20 20 40
1 C 40 30 20
2 D 30 40 60
2 E 10 50 50
2 F 20 60 30
;
run;

data temp(rename=(Month1_=Month1 Month2_=Month2 Month3_=Month3 name_=name));
length name_ $10.;
Month1_=0;
Month2_=0;
Month3_=0;

name_=' ';

 do until(last.grp);
	set have;
	by grp;
	Month1_=sum(Month1_,Month1);
	Month2_=sum(Month2_,Month2);
	Month3_=sum(Month3_,Month3);
	name_=cats(name_,name);
	
end;
name_=cats("SUM_",NAME_);

drop Month1 Month2 Month3 name;
run;

data want;
	set have temp;
	by grp;
	drop grp;
run;
Astounding
PROC Star

Here's a better, more flexible goal.  Add a fifth variable, GROUP, that will be 1 when NAME is A, B, or C.  GROUP will be 2 when NAME is D, E, or F.

 

This will easily let you create the report you want, and adds to your data the relationship between a NAME and a GROUP.

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 16. 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
  • 2 replies
  • 550 views
  • 0 likes
  • 3 in conversation