Previous Page | Next Page

Formats

Using Formats


Ways to Specify Formats

You can use formats in the following ways:


PUT Statement

The PUT statement with a format after the variable name uses a format to write data values in a DATA step. For example, this PUT statement uses the DOLLARw.d format to write the numeric value for AMOUNT as a dollar amount:

amount=1145.32;
put amount dollar10.2;

The DOLLARw.d format in the PUT statement produces this result:

$1,145.32

See PUT Statement for more information.


PUT Function

The PUT function converts a numeric variable, a character variable, or a constant using any valid format and returns the resulting character value. For example, the following statement converts the value of a numeric variable into a two-character hexadecimal representation:

num=15;
char=put(num,hex2.);

The PUT function returns a value of 0F, which is assigned to the variable CHAR.

The PUT function is useful for converting a numeric value to a character value. See PUT Function for more information.


%SYSFUNC

The %SYSFUNC (or %QSYSFUNC) macro function executes SAS functions or user-defined functions and applies an optional format to the result of the function outside a DATA step. For example, the following program writes a numeric value in a macro variable as a dollar amount.

 %macro tst(amount);
   %put %sysfunc(putn(&amount,dollar10.2));
 %mend tst;

 %tst (1154.23);

For more information, see SAS Macro Language: Reference.


FORMAT Statement

The FORMAT statement permanently associates a format with a variable. SAS uses the format to write the values of the variable that you specify. For example, the following statement in a DATA step associates the COMMAw.d numeric format with the variables SALES1 through SALES3:

format sales1-sales3 comma10.2;

Because the FORMAT statement permanently associates a format with a variable, any subsequent DATA step or PROC step uses COMMA10.2 to write the values of SALES1, SALES2, and SALES3. See FORMAT Statement for more information.

Note:   If you assign formats with a FORMAT statement before a PUT statement, all leading blanks are trimmed. Formats that are associated with variables by using a FORMAT statement behave like formats that are used with a colon (:) modifier in a subsequent PUT statement. For details about using the colon format modifier, see PUT Statement, List.   [cautionend]


ATTRIB Statement

The ATTRIB statement can also associate a format, as well as other attributes, with one or more variables. For example, in the following statement the ATTRIB statement permanently associates the COMMAw.d format with the variables SALES1 through SALES3:

attrib sales1-sales3 format=comma10.2;

Because the ATTRIB statement permanently associates a format with a variable, any subsequent DATA step or PROC step uses COMMA10.2 to write the values of SALES1, SALES2, and SALES3. See ATTRIB Statement for more information.


Permanent versus Temporary Association

When you specify a format in a PUT statement, SAS uses the format to write data values during the DATA step but does not permanently associate the format with a variable. To permanently associate a format with a variable, use a FORMAT statement or an ATTRIB statement in a DATA step. SAS permanently associates a format with the variable by modifying the descriptor information in the SAS data set.

Using a FORMAT statement or an ATTRIB statement in a PROC step associates a format with a variable for that PROC step, as well as for any output data sets that the procedure creates that contain formatted variables. For more information about using formats in SAS procedures, see Base SAS Procedures Guide.


User-Defined Formats

In addition to the formats that are supplied with Base SAS software, you can create your own formats. In Base SAS software, PROC FORMAT allows you to create your own formats for both character and numeric variables. For more information, see The FORMAT Procedure in Base SAS Procedures Guide.

When you execute a SAS program that uses user-defined formats, these formats should be available. The two ways to make these formats available are

To create permanent SAS formats, see The FORMAT Procedure in Base SAS Procedures Guide.

If you execute a program that cannot locate a user-defined format, the result depends on the setting of the FMTERR system option. If the user-defined format is not found, then these system options produce these results:

System Options Results
FMTERR SAS produces an error that causes the current DATA or PROC step to stop.
NOFMTERR SAS continues processing and substitutes a default format, usually the BESTw. or $w. format.

Although using NOFMTERR enables SAS to process a variable, you lose the information that the user-defined format supplies.

To avoid problems, make sure that your program has access to all user-defined formats that are used.

Previous Page | Next Page | Top of Page