Obsidian | Level 7

## Sum total row

I would like to add a total row to my table. Here is what I have:

 Gender Freq Percent Males 20 25% Females 60 75%

Here is what I'd like:

 Gender Freq Percent Males 20 25% Females 60 75% Total 80

Just want the total there as its own row. What's the best proc to do this, as I realize you can do this a number of ways (tabulate, sql, etc.)

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Sum total row

``````
data have;
input Gender \$ 	Freq 	Percent :percent.;
format percent percent.;
cards;
Males	20	25%
Females 	60	75%
;

proc sql;
select * from have
union all
select 'Total', sum(freq),. from have;
quit;
/*or*/
proc report data=have;
column gender freq percent;
define freq/analysis;
compute after;
line 'Total' +2 freq.sum ;
endcomp;
run;``````
8 REPLIES 8
Super User

## Re: Sum total row

FREQ with WEIGHT or FREQ statements as you'll also get the percentages automatically.
PROC PRINT will also do that...

The best proc depends on what you're doing next and why you're doing this in the first place.
Tourmaline | Level 20

## Re: Sum total row

``````
data have;
input Gender \$ 	Freq 	Percent :percent.;
format percent percent.;
cards;
Males	20	25%
Females 	60	75%
;

proc sql;
select * from have
union all
select 'Total', sum(freq),. from have;
quit;
/*or*/
proc report data=have;
column gender freq percent;
define freq/analysis;
compute after;
line 'Total' +2 freq.sum ;
endcomp;
run;``````
SAS Super FREQ

## Re: Sum total row

Hi:

Or, in the interest of completeness -- without a LINE statement and using an RBREAK statement, you can generate the report either with or without a total for percent:

Cynthia

Tourmaline | Level 20

## Re: Sum total row

Yes thought of it. Just seemed straight forward enough to use the computed memory value by the engine soon as a variable was defined analysis. Point taken. Thank you 🙂

SAS Super FREQ

## Re: Sum total row

Yes, it is straightforward, but the LINE statement may not align with the cells above as shown in the original pix of the desired output. The LINE statement output spans the whole table, and by default, the output from LINE is usually centered, so Total, would not always line up under Gender. So that's the side effect of RBREAK because RBREAK maintains the same number of cells on the break line.
Cynthia
Tourmaline | Level 20

## Re: Sum total row

Yes that makes sense. I agree! 🙂 Thank you!

Also , @Cynthia_sas  If you happen to know Jane Eslinger, please pass on my regards and gratitude to her for having authored that lovely proc report book. Kudos!!! I would say, a must read.

SAS Super FREQ

## Re: Sum total row

You're right - -it is a great book. I'll pass on your praise to her.
Cynthia
Super User

## Re: Sum total row

``````data have;
set sashelp.class;
run;

proc sql;
select sex,count(*) as n,calculated n/(select count(*) from have) as percent format=percent8.2
from have
group by sex
union all
select 'Total' ,(select count(*) from have),. from have(obs=1);
quit;``````
Discussion stats
• 8 replies
• 883 views
• 0 likes
• 5 in conversation