Using Formats


Ways to Specify Formats

You can use formats in the following ways:

  • in a PUT statement

  • with the PUT, PUTC, or PUTN functions

  • with the % SYSFUNC macro function

  • in a FORMAT statement in a DATA step or a PROC step

  • in an ATTRIB statement in a DATA step or a PROC step.

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 DOLLAR w . d format in the PUT statement produces this result:

 ,145.32 

See 'PUT Statement' on page 1342 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 Function' on page 754 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 COMMA w . 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' on page 1207 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 Statement, List' on page 1364.

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 COMMA w . 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' on page 1109 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 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, not temporary, formats with PROC FORMAT

  • to store the source code that creates the formats (the PROC FORMAT step) with the SAS program that uses them.

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 BEST w . 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.




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net