A Simple Proc Tabulate Example in SAS

Proc Tabulate Example in SASProc Tabulate is a useful SAS procedure to display statistics in tabular forms. It is easy to learn and lets you write advanced reports with little coding. In this example page, I will demonstrate a few simple Proc Tabulate examples. Also, I will demonstrate a few basic tricks to change the table to your liking. The form of all Tabulate Procedures is the same. Therefore, you can use the code examples here as templates for your actual data.

In the examples below, I use the sashelp.baseball data set.

One Dimensional Table

First, let us look at the simplest possible example. A one-dimensional table with a single class variable and no analysis variable. A Proc Tabulate step must include a Table Statement and a Class/Var Statement or both. In the example below, I create a one-dimensional table with a column for each Team and the number of observations within each team as the statistic.

proc tabulate data=sashelp.baseball;
   class Team;
   tables Team;
quit;

Two Dimensional Table

Next, let us add another dimension in Proc Tabulate. Now, we create a two-dimensional table. We add another dimension by separating the variables in the Table Statement with a comma. I want to know how many home-runs each team scores. Therefore, I specify nHome in the Var Statement. Also, I put nHome in the Table Statement after a comma. This creates a table with teams as rows and the sum of home-runs as a column. A natural question is this: How do I know if a variable will produce rows or columns? A rule of thumb is this. The variable(s) closed to the semicolon will always make columns. Regardless of the number of dimensions.

proc tabulate data=sashelp.baseball;
   class Team;
   var nHome;
   tables Team, nHome;
quit;

Three Dimensional Table

Finally, let us create a three-dimensional table. This is the maximum number of dimensions available in Proc Tabulate. The natural way to think of a three-dimensional table is a cubic form. However, the reality is simpler. It simply means that we create a table for each level of the variable furthest to the left. Run the code below. It creates two tables. One for each value of League.

proc tabulate data=sashelp.baseball;
   class Team League;
   var nHome;
   tables League, Team, nHome;
quit;

Control SAS Statistics in Proc Tabulate

In the snippets above, we rely on the default statistics of Proc Tabulate. However, we can easily control the statistic we want to display. We do so by using an asterisc and the name of the desired statistic. You can see a list of available statistics here. The default statistic for class variables is count (n). The default statistic for an analysis variable in sum.

proc tabulate data=sashelp.baseball;
   class Team;
   var nHome;
   tables Team, nHome*n;
quit;

Control Headers in Proc Tabulate

When you run Proc Tabulate in SAS, it creates headers and labels for all the variables in your Table Statement. This may not be what you want. Especially if your table is large and has many variables. After each variable, you can specify an equal sign and the value of the header/label you want to appear in the report. If you do not want a header at all, simply put =””.

proc tabulate data=sashelp.baseball;
   class Team;
   var nHome;
   tables Team="", nHome="Home Runs"*n="";
quit;

Summary

In this example page, I provide a gentle introduction to Proc Tabulate in SAS. We learn about the syntax of the procedure and how to create different dimensions. Furthermore, I present how to control statistics and headers in the table. I encourage you to play around with the code snippets above. Also, read the documentation when you get an error.

If you want to learn more about Proc Tabulate in SAS, I recommend the book PROC TABULATE by Example. Easy to read and packed with examples.

You can download the entire code from this example page here.