BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

What is the way to calculate percentage of X and of W for each group (group is defined by Z1).

I want to calculate it via proc report and display the report via proc report

Thanks

 
Data have;
input z1 $ Z2 x w;
cards;
A 1 10 20
A 2 20 30
A 3 40 50
B 1 15 30
B 2 25 30
B 3 40 50
;
run;
proc sql;
create table t1 as
select sum(X) as TOTAL_X,
sum(w) as TOTAL_w
from have
;
quit;
data t2;
set have ;
if _n_=1 then set t1;
PCT_X=X/TOTAL_X;
PCT_W=W/TOTAL_w;
format PCT_X PCT_W percent8.1;
Run;
proc report data=t2;
run;

 

2 REPLIES 2
Kurt_Bremser
Super User

Define both z1 and z2 as group, calculate sums for groups defined by z1, and the compute the percentages:

data have;
input z1 $ z2 x w;
cards; 
A 1 10 20
A 2 20 30
A 3 40 50
B 1 15 30
B 2 25 30
B 3 40 50
;

proc report data=have;
column z1 z2 x pct_x w pct_w;
define z1 / group;
define z2 / group;
define x / analysis;
define pct_x / computed format=percent8.2;
define w / analysis;
define pct_w / computed format=percent8.2;
compute before z1;
  x_sum = x.sum;
  w_sum = w.sum;
endcomp;
compute pct_x;
  pct_x = x.sum / x_sum;
endcomp;
compute pct_w;
  pct_w = w.sum / w_sum;
endcomp;
run;

Result:

z1	z2	x	pct_x	w	pct_w
A	1	10	14.29%	20	20.00%
 	2	20	28.57%	30	30.00%
 	3	40	57.14%	50	50.00%
B	1	15	18.75%	30	27.27%
 	2	25	31.25%	30	27.27%
 	3	40	50.00%	50	45.45%
Ksharp
Super User

I would prefer to use PROC SQL rather than PROC REPORT to get it .

 

 
Data have;
input z1 $ Z2 x w;
cards; 
A 1 10 20
A 2 20 30
A 3 40 50
B 1 15 30
B 2 25 30
B 3 40 50
;
run;

proc report data=have nowd;
columns z1 z2 x w total_x total_w pct_x pct_w;
define z1/display;
define z2/display;
define x/analysis sum;
define w/analysis sum;
define total_x/computed;
define total_w/computed;
define pct_x/computed format=percent7.1;
define pct_w/computed format=percent7.1;

compute before ;
x_sum=x.sum;
w_sum=w.sum;
endcomp;
compute total_x;
total_x=x_sum;
endcomp;
compute total_w;
total_w=w_sum;
endcomp;
compute pct_x;
pct_x=x.sum/x_sum;
endcomp;
compute pct_w;
pct_w=w.sum/w_sum;
endcomp;
run;

Ksharp_0-1739150677444.png

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 933 views
  • 4 likes
  • 3 in conversation