BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Q1983
Lapis Lazuli | Level 10
data baseball(keep=Team nAtBat nHits);
set sashelp.baseball;
if team in ('Cleveland','Houston','Seattle','Montreal');
run;

proc tabulate data= baseball;
class Team;
var nAtBat
	nHits ;
Table Team =' ' all={label='Grand Total' 
S=[background = lightblue cellwidth=80]} 
*[STYLE=[Font_Weight=BOLD]],
nAtBat*(sum*f=comma15.)
nHits*(sum*f=comma15.);
label nAtBat = 'Total At Bats';
label nHits = 'Total Hits';
;run;

The proc tabulate prouduces the following.  The objective is to get the percentage of hits to at bats and the bottomline average

  Total At Bats Total Hits  
Sum Sum  
Cleveland 5,478 1,564  
Houston 4,386 1,201  
Montreal 4,539 1,216  
Seattle 4,975 1,279  
Grand Total 19,378 5,260  
       
       
Desire to get the percentage of the total at bats
  Total At Bats Total Hits Pct Hits to At bats
Sum Sum  
Cleveland 5,478 1,564 28.55%
Houston 4,386 1,201 27.38%
Montreal 4,539 1,216 26.79%
Seattle 4,975 1,279 25.71%
Grand Total 19,378 5,260 27.14%
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Perhaps:

 

proc tabulate data= baseball;
   class Team;
   var nAtBat
   	nHits ;
   Table Team =' ' all='Grand Total' ,
   nAtBat*(sum*f=comma15.) nHits*(sum*f=comma15.  pctsum<natbat>)

   ;
   label nAtBat = 'Total At Bats';
   label nHits = 'Total Hits';
   
run;

You can provide another variable in the table and use Pctsum<variable> or Pctn<variable> to provide the sum or count of the the other variable as the denominator for percent calculations. There are restrictions about where these can go in relation to specific table structure. The <> characters are required.

 

When the question is how to calculate something please don't include a bunch of extra "style" elements that obscure the details of the question.

 

BTW there really isn't a need to create a data set for that simple subset of the data. You could use a where statement in Proc tabulate like

where team in ('Cleveland','Houston','Seattle','Montreal');

with the sashelp.baseball as the data set in the Proc Tabulate statement.

 

View solution in original post

1 REPLY 1
ballardw
Super User

Perhaps:

 

proc tabulate data= baseball;
   class Team;
   var nAtBat
   	nHits ;
   Table Team =' ' all='Grand Total' ,
   nAtBat*(sum*f=comma15.) nHits*(sum*f=comma15.  pctsum<natbat>)

   ;
   label nAtBat = 'Total At Bats';
   label nHits = 'Total Hits';
   
run;

You can provide another variable in the table and use Pctsum<variable> or Pctn<variable> to provide the sum or count of the the other variable as the denominator for percent calculations. There are restrictions about where these can go in relation to specific table structure. The <> characters are required.

 

When the question is how to calculate something please don't include a bunch of extra "style" elements that obscure the details of the question.

 

BTW there really isn't a need to create a data set for that simple subset of the data. You could use a where statement in Proc tabulate like

where team in ('Cleveland','Houston','Seattle','Montreal');

with the sashelp.baseball as the data set in the Proc Tabulate statement.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 798 views
  • 3 likes
  • 2 in conversation