SAS Formats SAS How To's

How to Format Variables in PROC MEANS, FREQ, and TABULATE in SAS

In this article, we discuss how to format variables in the most used SAS procedures, namely PROC MEANS, PROC FREQ, and PROC TABULATE.

Generally speaking, you use the FORMAT statement to format a variable in SAS procedures. The FORMAT statement starts with the FORMAT keyword followed by the variable you want to format, and the format name. Yet, it depends on the procedure which variables you can format.

In this article, we demonstrate how to associate variables with formats in procedures. However, we’ve also written an article about formatting variables in a SAS dataset (or table).

We will use examples to show how to format data in SAS procedures. The data comes from the CARS dataset in the SASHELP library. This dataset contains information about cars, such as the invoice and the drive train.

How to Format PROC MEANS

One of the most used SAS procedures to quickly analyze data is the PROC MEANS procedure. By default, this procedure provides five basic descriptive statistics, namely the:

The SAS code generates a standard PROC MEANS report for the variable invoice.

proc means data=sashelp.cars;
    var invoice;
run;
Default PROC MEANS report

Although the format dollar8. was associated with the variable invoice in the original dataset, the report generated by PROC MEANS doesn’t contain any formating. In fact, PROC MEANS provides a limited number of formatting options. You can only limit the number of decimals and format (if applicable) the character variables that define groups (i.e., classes).

How to Limit the Number of Decimals in PROC MEANS

One of the few options PROC MEANS provides to change the appearance of the report is the MAXDEC=-option.

The MAXDEC=-option limits the number of decimals SAS displays in the PROC MEANS report. You can choose between a minimum of 0 decimals and a maximum of 8. Moreover, the MAXDEC=-option also rounds variables to the nearest multiple of the desired number of decimals.

The MAXDEC=-option is part of the PROC MEANS statement.

The SAS code below shows an example of how to use the MAXDEC=-option and round all data to the nearest integer.

proc means data=sashelp.cars maxdec=0;
    var invoice;
run;
Format the number of decimals in PROC MEANS.

Instead of rounding numbers in PROC MEANS, you can also round numbers in a SAS Data Step or in the PROC SQL procedure.

How to Format the Class Variable in PROC MEANS

Although you can’t format the descriptive statistics (N, Mean, Std Dev, etc.), you can associate a format to the CLASS variables.

The CLASS variables (and CLASS statement) are used to group data and calculate the descriptive statistics per group. For example, in the image below we used the variable drivetrain to group the data.

If the values of the CLASS variable are undescriptive, you can use the FORMAT statement to associate a format with them. Since the CLASS variables are normally character variables, it’s most likely that you use a user-written format to change the appearance.

The FORMAT statement starts with the FORMAT keyword followed by the variable and the desired format.

In the example below, we create a user-written format and use the FORMAT statement to change the appearance of the variable drivetrain.

proc format;
    value $drivetrain_fmt
	"All" = "All-wheel Drive (AWD)"
	"Front" = "Front-wheel Drive (FWD)"
	"Rear" = "Rear-wheel Drive (RWD)";
run;
 
proc means data=sashelp.cars;
    class drivetrain;
    var invoice;
    format drivetrain $drivetrain_fmt.;
run;
Format the Class Variable in PROC MEANS.

How to Format PROC FREQ

Another frequently used procedure in SAS is the PROC FREQ procedure. The procedure creates one-way and n-way frequency tables, for example:

proc freq data=sashelp.cars;
    tables drivetrain;
run;
Default PROC FREQ report

In general, the PROC FREQ procedure provides limited possibilities to format its output. However, there are still two useful options.

How to Format the Class Variable in PROC FREQ

The first option associates a format with the variable you are counting. For example, below we use the PROC FREQ procedure to count the number of occurrences of each value in the variable drivetrain.

proc format;
    value $drivetrain_fmt
	"All" = "All-wheel Drive (AWD)"
	"Front" = "Front-wheel Drive (FWD)"
	"Rear" = "Rear-wheel Drive (RWD)";
run;
 
proc freq data=sashelp.cars;
    tables drivetrain;
    format drivetrain $drivetrain_fmt.;
run;
Format the Class Variable in PROC FREQ.

If you don’t like the values SAS displays, you can use the FORMAT statement. Because you normally count occurrences of character variables, the new format is generally a user-written format.

How to Order Variables in PROC FREQ

The second way to format the frequency table is by specifying the order of the data.

By default, PROC FREQ orders data in alphabetical order. However, you can change this behavior with the ORDER=-option. To order the frequency table from high to low (i.e., descendingly) you can use the option ORDER=freq. Other possibilities are ORDER=formatted and ORDER=data.

In the example below we show how to use the ORDER=-option and sort the frequency table descendingly.

proc freq data=sashelp.cars order=freq;
    tables drivetrain;
run;
Format the Order of the Variables in PROC FREQ.

If you want to know more about frequency tables (e.g., creating an output table), then we recommend this article.

How to Format PROC TABULATE

In contrast to the previous two procedures, the PROC TABULATE procedure provides many formatting options. Besides, you can use PROC TABULATE also to calculate different descriptive statistics.

The following SAS code creates a standard PROC TABULATE report that shows the mean and standard deviation of the invoice per drivetrain.

proc tabulate data=sashelp.cars;
    class drivetrain;
    var invoice;
    table drivetrain,
	(invoice)*(mean std);
run;
Default PROC TABULATE report.

We discuss three options to change the appearance of the PROC TABULATE report.

How to Format Numeric Variables in PROC TABULATE

Firstly, you can use the FORMAT=-option to associate a variable with a format.

The FORMAT=-option is part of the TABLE statement and must be written between braces (i.e., curly brackets). Additionally, you can use the braces also to define text color, background color, and text size.

In the example below we use the FORMAT=-option of the PROC TABULATE procedure to associate a format with the variable invoice.

proc tabulate data=sashelp.cars;
    class drivetrain;
    var invoice;
    table drivetrain,
	(invoice)*(mean std)*{format=dollar12.2};
run;
Format Numeric Variables in PROC TABULATE.

How to Format the Class Variable in PROC TABULATE

Secondly, you use the FORMAT statement to format character variables.

The FORMAT statement works as usual and must be placed after the TABLE statement. You can use multiple FORMAT statements to enhance the appearance of the PROC TABULATE report.

For example, below we associated a user-written format to the variable drivetrain.

proc format;
    value $drivetrain_fmt
	"All" = "All-wheel Drive (AWD)"
	"Front" = "Front-wheel Drive (FWD)"
	"Rear" = "Rear-wheel Drive (RWD)";
run;
 
proc tabulate data=sashelp.cars;
    class drivetrain;
    var invoice;
    table drivetrain,
	(invoice)*(mean std);
    format drivetrain $drivetrain_fmt.;
run;
Format the Class Variables in PROC TABULATE.

How to Suppress Row Headings in PROC TABULATE

Lastly, a frequently used option is the INDENT=-option

The INDENT=-option can be used to suppress the row headings in the report of PROC TABULATE. This is especially useful if you export the report to Microsoft Excel. To remove the row headings, you use INDENT=0.

The INDENT=-option is part of the TABLE statement and must be written at the end of the statement after a forward-slash (“/”).

Next we provide an example.

proc tabulate data=sashelp.cars;
    class drivetrain;
    var invoice;
    table drivetrain,
	(invoice)*(mean std)
    /indent=0;
run;
Remove the Class Heading in PROC TABULATE.