Chapter Contents

Previous

Next
SAS Language Reference: Dictionary

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 DOLLAR. 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 for more information.

PUT Function

The PUT function writes a numeric variable, a character variable, or a constant with 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 creates a character variable named CHAR that has a value of 0F.

The PUT function is useful for converting a numeric value to a character value. See PUT 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 for more information.

Note:   Formats that you specify in a PUT statement behave differently from those that you associate with a variable in a FORMAT statement. The major difference is that formats that are specified in the PUT statement preserve leading blanks. If you assign formats with a FORMAT statement prior to a PUT statement, all leading blanks are trimmed. The result is the same as if you used the colon (:) format modifier. For details about using the colon (:) format modifier, see PUT, 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 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 on using formats in SAS procedures, see the 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 the 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 the 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.


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.