BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

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

 

2 REPLIES 2
Jagadishkatam
Amethyst | Level 16
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
Cynthia_sas
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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