6.8 - Formatting Data Values

You might recall that informats are used to tell SAS how to read special data values into your SAS data sets, and formats are used to tell SAS how to display those special data values in your reports. As you might recall from your prior (but admittedly brief) work with dates, when SAS stores special data values, it doesn't necessarily store numbers that would be meaningful to a casual reader of your reports. As a result, you have to use a FORMAT statement to tell SAS to display the stored numbers in a way that is meaningful to you and your readers. Let's look at an example of the use of the FORMAT statement in the PRINT procedure.

Example 6.20 Section

The following SAS program illustrates the use of the FORMAT statement to tell SAS to display the expense variable using the dollar9.2 format:

PROC PRINT data = basic LABEL;
   label name = 'Name'
         clinic = 'Clinic'
         expense = 'Expense';
   format expense dollar9.2;
   id name;
   var clinic expense;
Name Clinic Expense
Alice Smith LEWN $1,001.98
Maryann White LEWN $2,999.34
Thomas Jones ALTO $3,904.89
Benedictine Arnold ALTO $1,450.23
Felicia Ho MNMC $1,209.94
John Smith MNMC $1,763.09
Jane Smiley MNMC $3,567.00

The FORMAT statement tells SAS to associate, for the duration of the PRINT procedure, the dollar9.2 format with the expense variable. As soon as the PRINT procedure closes, the association no longer holds. The dollar9.2 format tells SAS to display the expense values using dollar signs, commas (when appropriate), and two decimal places. The 9 tells SAS that it will need at most 9 spaces to accommodate each expense value — 1 for the dollar sign, 1 for the comma sign, 4 for the digits before the decimal place, 1 for the decimal place, and 2 for the decimal place digits.

Launch and run the SAS program, and review the resulting output to convince yourself the expense variable values were printed as described. Then, you might want to change the 9 in the dollar9.2 format to an 8, and re-run the SAS program to see that SAS drops the comma in order to fit the values into the eight allocated spaces. Then, if you're still having fun, you might want to change the entire dollar9.2 format to the comma8.2 format, and re-run the SAS program to familiarize yourself with the comma format.

In general, you can use a separate FORMAT statement for each variable, or you can format several variables in a single FORMAT statement. The table below illustrates some of the most commonly used SAS formats:

Format Specifies These Values Example
COMMAw.d that contain commas and decimal places comma8.2
DOLLARw.d that contain dollar signs, commas, and decimal places dollar6.2
MMDDYYw. as date values of the form 10/03/08 (mmddyy8.) or 10/03/2008 (mmddyy10.) mmddyy10.
w. rounded to the nearest integer in w spaces 7.
w.d rounded to d decimal places in w spaces 8.2
$w. as character values in w spaces $12.
DATEw. as date values of the form 02OCT08 (date7.) or 02OCT2008 (date9.) date9.

Of course, you can find the other formats that are available using the SAS Help and Documentation.