BookmarkSubscribeRSS Feed
Swati24
Obsidian | Level 7

I have a data like this :

 

 

data one;
  infile datalines;
  input Group_Name: $12. Head_Name :$12. LastYear_Count BeforeLastYear_Count;
 datalines;
R&D IT 1 2
R&D Testing 0 5
R&D Production 4 0
R&D Development 1 0
Sales Book 5 4
Sales Product 10 0
Sales Kit 1 1
Admin Marketing 4 1
Admin HR 5 2
Admin Project 6 3
;
run;

I need the output like this:

 

Group_NameHead_NameLastYear_CountBeforeLastYear_Count
R&DIT12
R&DTesting05
R&DProduction40
R&DDevelopment10
SubTotal 67
SalesBook54
SalesProduct100
SalesKit11
SubTotal 165
AdminMarketing41
AdminHR52
AdminProject63
SubTotal 156

 

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16
data one;
  infile datalines;
  input Group_Name: $12. Head_Name :$12. LastYear_Count BeforeLastYear_Count;
 datalines;
R&D IT 1 2
R&D Testing 0 5
R&D Production 4 0
R&D Development 1 0
Sales Book 5 4
Sales Product 10 0
Sales Kit 1 1
Admin Marketing 4 1
Admin HR 5 2
Admin Project 6 3
;
run;

proc sort data=one;
by Group_Name;
run;

data want;
set one;
by Group_Name;
output;
retain LastYear_Count_ BeforeLastYear_Count_;
if first.Group_Name then do; LastYear_Count_=.;BeforeLastYear_Count_=.;end;
LastYear_Count_+LastYear_Count;
BeforeLastYear_Count_+BeforeLastYear_Count;
Group_Name='Sub Total';
if last.Group_Name then do;LastYear_Count=LastYear_Count_;BeforeLastYear_Count=BeforeLastYear_Count_;
output;
end;
drop BeforeLastYear_Count_ LastYear_Count_;
run;
Thanks,
Jag
Swati24
Obsidian | Level 7
Thanks but this will not work as after each group value one more row should be added that is subtotal.
learsaas
Quartz | Level 8

无标题.jpg

Jagadishkatam
Amethyst | Level 16

I believe it is giving the expected output the only update is with regard to making Head_Name='';

could you please check now and let me know. 

I know you got the solution through proc report but would like to know if this also works.

 

data want;
set one;
by Group_Name;
output;
retain LastYear_Count_ BeforeLastYear_Count_;
if first.Group_Name then do; LastYear_Count_=.;BeforeLastYear_Count_=.;end;
LastYear_Count_+LastYear_Count;
BeforeLastYear_Count_+BeforeLastYear_Count;
Group_Name='Sub Total';
Head_Name='';
if last.Group_Name then do;LastYear_Count=LastYear_Count_;BeforeLastYear_Count=BeforeLastYear_Count_;
output;
end;
drop BeforeLastYear_Count_ LastYear_Count_;
run;
Thanks,
Jag
Swati24
Obsidian | Level 7
Thanks I got the solution by using below Proc Report code:
proc report data=one;
column group_name head_name Before_Last_Year Last_Year;
define group_name / group;
define head_name / group;
define Before_Last_Year / sum;
define Last_Year / sum;
break after group_name / summarize style={background=lightyellow};
rbreak after / summarize style=Header;
compute after group_name;
group_name = 'Sub Total';
endcomp;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please use the search functionality before posting, there are hundreds of posts already on this topic.  This one from last week for instance:

https://communities.sas.com/t5/SAS-Programming/insert-a-row-at-the-end-of-specific-rows-with-Totals/...

singhsahab
Lapis Lazuli | Level 10

Hi ,

 

data one;
  infile datalines;
  input Group_Name: $12. Head_Name :$12. LastYear_Count BeforeLastYear_Count;
 datalines;
R&D IT 1 2
R&D Testing 0 5
R&D Production 4 0
R&D Development 1 0
Sales Book 5 4
Sales Product 10 0
Sales Kit 1 1
Admin Marketing 4 1
Admin HR 5 2
Admin Project 6 3
;
run;

proc report data=one out=want1;
column  group_name Head_Name LastYear_Count BeforeLastYear_Count;
define group_name/order;
define head_name/display;
define LastYear_Count/sum;
define BeforeLastYear_Count/sum;
break after group_name/summarize;
run;

data final (drop=_break_);
set want1;
by group_name ;
if last.group_name then group_name='totel';
run;

Thanks 🙂 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 7 replies
  • 1829 views
  • 1 like
  • 5 in conversation