BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Krysia24
Obsidian | Level 7

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

 

Gender Freq Percent 
Males2025%
Females 6075%

 

Here is what I'd like:

Gender FreqPercent 
Males2025%
Females 6075%
Total80 

 

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
novinosrin
Tourmaline | Level 20

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;

View solution in original post

8 REPLIES 8
Reeza
Super User
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.
novinosrin
Tourmaline | Level 20

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;
Cynthia_sas
SAS Super FREQ

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_sas_0-1611193388298.png

Cynthia

novinosrin
Tourmaline | Level 20

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 🙂

Cynthia_sas
SAS Super FREQ
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
novinosrin
Tourmaline | Level 20

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.

Cynthia_sas
SAS Super FREQ
You're right - -it is a great book. I'll pass on your praise to her.
Cynthia
Ksharp
Super User
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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 2047 views
  • 0 likes
  • 5 in conversation