The SAS Output Delivery System and reporting techniques

Proc Report Group Totals by Year Group

Reply
Frequent Contributor
Posts: 143

Proc Report Group Totals by Year Group

[ Edited ]

data test;

infile datalines;

input Year $ Dept1N Dept2N ;

return;

datalines;

2014 2500 2100

2014 200 2100

2014 4400 2100

2015 7700 4130

2015 3320 4110

2015 3660 4150

;

run;

data test2;

set test;

DeptN=Dept1N+Dept2N;

DeptPct=Dept1N/DeptN*100;

run;

 

proc report =test2;

column Year Dept1N Dept2N DeptN DeptPct;

define Year /group;

define Dept1N /group;

compute after Year;

endcomp;

run;

 

I get this

Year Dept1N Dept2N DeptN DeptPct
2014 200 2100 2300 8.6956522
  2500 2100 4600 54.347826
  4400 2100 6500 67.692308
2015 3320 4110 7430 44.683715
  3660 4150 7810 46.862996
  7700 4130 11830 65.088757

 

However I want this output, summarized by group

Year Dept1N Dept2N DeptN DeptPct          
2014 2500 2100 4600 54.34783          
2014 200 2100 2300 8.695652          
2014 4400 2100 6500 67.69231          
2014 7100 6300 13400 43.5786 Group totals each year and average for DeptPct
2015 7700 4130 11830 65.08876          
2015 3320 4110 7430 44.68371          
2015 3660 4150 7810 46.863          
2014 14680 12390 27070 52.21182 Group totals each year and average for DeptPct

 

Since I already performed the calculations in my datastep I would think proc report is the way to go.  I eventually want to add to an ODS tagset format

 

Trusted Advisor
Posts: 1,137

Re: Proc Report Group Totals by Year Group

proc report data =test2;
column Year (Dept1N Dept2N DeptN DeptPct);
define Year / display order ;
define DeptN /  analysis sum;
define Dept1N /  analysis sum;
define Dept2N / analysis sum;
define DeptPct /  analysis  mean;
break after year/ summarize skip ul ;
run;
Thanks,
Jag
SAS Super FREQ
Posts: 8,868

Re: Proc Report Group Totals by Year Group

[ Edited ]
Posted in reply to Jagadishkatam

Hi:

  And for a further refinement, there is actually no need to create the TEST2 data set at all. Once you change the usage of the DEPT1N and DEPT2N variables to ANALYSIS, they will not maintain the desired order on the report, but that can be fixed with a  PROC SORT.

 

  There's no need for the divide in the DATA step or the multiply by 100 -- the PERCENT format will take care of the multiply by 100 and PROC REPORT will do all of the calculations in COMPUTE blocks, as shown below:

no_need_for_test2.png

 

And, it is possible to consolidate the 3 separate COMPUTE blocks into 1 COMPUTE block, as shown below (results would be same as #2):

with_one_compute.png

cynthia

Ask a Question
Discussion stats
  • 2 replies
  • 257 views
  • 2 likes
  • 3 in conversation