Applying SAS Formats and Informats

Temporarily Assigning Formats to Variables

In your SAS reports, formats control how the data values are displayed. To make data values more understandable when they are displayed in your procedure output, you can use the FORMAT statement, which associates formats with variables.
Formats affect only how the data values appear in output, not the actual data values as they are stored in the SAS data set.
Syntax, FORMAT statement:
FORMAT variable(s) format-name;
  • variable(s) is the name of one or more variables whose values are to be written according to a particular pattern
  • format-name specifies a SAS format or a user-defined format that is used to write out the values.
Tip:The FORMAT statement applies only to the PROC step in which it appears.
You can use a separate FORMAT statement for each variable, or you can format several variables (using either the same format or different formats) in a single FORMAT statement.
Table 12.1 Formats That Are Used to Format Data
FORMAT Statement
Description
Example
format date mmddyy8.;
associates the format MMDDYY8. with the variable Date
01/06/17
format net comma5.0
       gross comma8.2;
associates the format COMMA5.0 with the variable Net and the format COMMA8.2 with the variable Gross
1,234
5,678.90
format net gross dollar9.2;
associates the format DOLLAR9.2 with both variables, Net, and Gross
$1,234.00
$5,678.90
For example, the FORMAT statement below writes values of the variable Fee using dollar signs, commas, and no decimal places.
proc print data=cert.admit;
  var actlevel fee;
  where actlevel='HIGH';
  format fee dollar4.;
run;
Figure 12.1 FORMAT Statement Output
FORMAT Statement Example

Specifying SAS Formats

The table below describes some SAS formats that are commonly used in reports.
Table 12.2 Commonly Used SAS Formats
Format
Description
Example
COMMAw.d
specifies values that contain commas and decimal places
comma8.2
DOLLARw.d
specifies values that contain dollar signs, commas, and decimal places
dollar6.2
MMDDYYw.
specifies values as date values of the form 09/12/17 (MMDDYY8.) or 09/12/2017 (MMDDYY10.)
mmddyy10.
w.
specifies values that are rounded to the nearest integer in w spaces
7.
w.d
specifies values that are rounded to d decimal places in w spaces
8.2
$w.
specifies values as character values in w spaces
$12.
DATEw.
specifies values as date values of the form 16OCT17 (DATE7.) or 16OCT2017 (DATE9.)
date9.

Field Widths

All SAS formats specify the total field width (w) that is used for displaying the values in the output. For example, suppose the longest value for the variable Net is a four-digit number, such as 5400. To specify the COMMAw.d format for Net, you specify a field width of 5 or more. You must count the comma, because it occupies a position in the output.
Note: When you use a SAS format, specify a field width (w) that is wide enough for the largest possible value. Otherwise, values might not be displayed properly.
Figure 12.2 Specifying a Field Width (w) with the FORMAT Statement
Specifying a Field Width (w) with the FORMAT Statement

Decimal Places

For numeric variables, you can also specify the number of decimal places (d), if any, to be displayed in the output. Numbers are rounded to the specified number of decimal places. In the example above, no decimal places are displayed.
Writing the whole number 2030 as 2,030.00 requires eight print positions, including two decimal places and the decimal point.
Figure 12.3 Whole Number Decimal Places
Whole Number Decimal Places
Formatting 15374 with a dollar sign, commas, and two decimal places requires 10 print positions.
Figure 12.4 Specifying 10 Decimal Places
Specifying 10 Decimal Places

Examples: Data Values and Formats

This table shows you how data values are displayed when different format, field width, and decimal place specifications are used.
Table 12.3 Displaying Data Values with Formats
Stored Value
Format
Displayed Value
38245.3975
COMMA9.2
38,245.40
38245.3975
8.2
38245.40
38245.3975
DOLLAR10.2
$38,245.40
38245.3975
DOLLAR9.2
$38245.40
38245.3975
DOLLAR8.2
38245.40
0
MMDDYY8.
01/01/60
0
MMDDYY10.
01/01/1960
0
DATE7.
01JAN60
0
DATE9.
01JAN1960
Tip
If a format is too small, the following message is written to the SAS log:
NOTE: At least one W.D format was too small for the number to be printed. The decimal might be shifted by the 'BEST' format.
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.168.214