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% |
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
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.
Ready to level-up your skills? Choose your own adventure.