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.

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