FORMAT Procedure

Concepts: FORMAT Procedure

Associating Informats and Formats with Variables

Methods of Associating Informats and Formats with Variables

The following table summarizes the different methods for associating informats and formats with variables.
Associating Informats and Formats with Variables
Step
Informats
Formats
In a DATA step
Use the ATTRIB or INFORMAT statement to permanently associate an informat with a variable. Use the INPUT function or INPUT statement to associate the informat with the variable only for the duration of the DATA step.
Use the ATTRIB or FORMAT statement to permanently associate a format with a variable. Use the PUT function or PUT statement to associate the format with the variable only for the duration of the DATA step.
In a PROC step
The ATTRIB and INFORMAT statements are valid in Base SAS procedures. However, in Base SAS software, typically you do not assign informats in PROC steps because the data has already been read into SAS variables.
Use the ATTRIB statement or the FORMAT statement to associate formats with variables. If you use either statement in a procedure that produces an output data set, then the format is permanently associated with the variable in the output data set. If you use either statement in a procedure that does not produce an output data set or modify an existing data set, the statement associates the format with the variable only for the duration of the PROC step.

Differences between the FORMAT Statement and PROC FORMAT

Do not confuse the FORMAT statement with the FORMAT procedure. The FORMAT and INFORMAT statements associate an existing format or informat (either standard SAS or user-defined) with one or more variables. PROC FORMAT creates user-defined formats or informats.

Assigning Formats and Informats to a Variable

Assigning your own format or informat to a variable is a two-step process: creating the format or informat with the FORMAT procedure, and then assigning the format or informat with the FORMAT, INFORMAT, or ATTRIB statement.
It is often useful to assign informats in the FSEDIT procedure in SAS/FSP software and in the BUILD procedure in SAS/AF software.
For complete documentation on the ATTRIB, INFORMAT, and FORMAT statements, see SAS Statements: Reference. For complete documentation on the INPUT and PUT functions, see SAS Functions and CALL Routines: Reference. See Formatted Values for more information and examples of using formats in Base SAS procedures.

Storing Informats and Formats

Format Catalogs

PROC FORMAT stores user-defined informats and formats as entries in SAS catalogs.(footnote1) You use the LIBRARY= option in the PROC FORMAT statement to specify the catalog. If you omit the LIBRARY= option, then formats and informats are stored in the WORK.FORMATS catalog. If you specify LIBRARY=libref but do not specify a catalog name, then formats and informats are stored in the libref.FORMATS catalog. Note that this use of a one-level name differs from the use of a one-level name elsewhere in SAS. With the LIBRARY= option, a one-level name indicates a library; elsewhere in SAS, a one-level name indicates a file in the WORK library.
The name of the catalog entry is the name of the format or informat. The entry types are as follows:
  • FORMAT for numeric formats
  • FORMATC for character formats
  • INFMT for numeric informats
  • INFMTC for character informats

Temporary Informats and Formats

Informats and formats are temporary when they are stored in a catalog in the WORK library. If you omit the LIBRARY= option, then PROC FORMAT stores the informats and formats in the temporary catalog WORK.FORMATS. You can retrieve temporary informats and formats only in the same SAS session or job in which they are created. To retrieve a temporary format or informat, simply include the name of the format or informat in the appropriate SAS statement. SAS automatically looks for the format or informat in the WORK.FORMATS catalog.

Permanent Informats and Formats

If you want to use a format or informat that is created in one SAS job or session in a subsequent job or session, then you must permanently store the format or informat in a SAS catalog.
You permanently store informats and formats by using the LIBRARY= option in the PROC FORMAT statement. See the discussion of the LIBRARY= option in the PROC FORMAT Statement.

Accessing Permanent Informats and Formats

After you have permanently stored an informat or format, you can use it in later SAS sessions or jobs. If you associate permanent informats or formats with variables in a later SAS session or job, then SAS must be able to access the informats and formats. Thus, you must use a LIBNAME statement to assign a libref to the library that stores the catalog that stores the informats or formats.
SAS uses one of two methods when searching for user-defined formats and informats:
  • By default, SAS always searches a library that is referenced by the LIBRARY libref for a FORMATS catalog. If you have only one format catalog, then do the following:
    1. Assign the LIBRARY libref to a SAS library in the SAS session in which you are running the PROC FORMAT step.
    2. Specify LIBRARY=LIBRARY in the PROC FORMAT statement. PROC FORMAT stores the informats and formats that are defined in that step in the LIBRARY.FORMATS catalog.
    3. In the SAS program that uses your user-defined formats and informats, include a LIBNAME statement to assign the LIBRARY libref to the library that contains the permanent format catalog.
  • If you have more than one format catalog, or if the format catalog is named something other than FORMATS, then do the following:
    1. Assign a libref to a SAS library in the SAS session in which you are running the PROC FORMAT step.
    2. Specify LIBRARY=libref or LIBRARY=libref.catalog in the PROC FORMAT step, where libref is the libref that you assigned in step 1.
    3. In the SAS program that uses your user-defined formats and informats, use the FMTSEARCH= option in an OPTIONS statement, and include libref or libref.catalog in the list of format catalogs.
The syntax for specifying a list of format catalogs to search is
OPTIONS FMTSEARCH=(catalog-specification-1<… catalog-specification-n>);
Each catalog-specification can be libref or libref.catalog. If only libref is specified, then SAS assumes that the catalog name is FORMATS.
When searching for a format or informat, SAS always searches in WORK.FORMATS first, and then LIBRARY.FORMATS, unless one of them appears in the FMTSEARCH= list. SAS searches the catalogs in the FMTSEARCH= list in the order in which they are listed until the format or informat is found.
For more information, see FMTSEARCH= System Option in SAS System Options: Reference. For an example that uses the LIBRARY= and FMTSEARCH= options together, see Writing Ranges for Character Strings.

Missing Informats and Formats

If you reference an informat or format that SAS cannot find, then you receive an error message and processing stops unless the SAS system option NOFMTERR is in effect. When NOFMTERR is in effect, SAS uses the w. or $w. default format to print values for variables with formats that it cannot find. For example, to use NOFMTERR, use this OPTIONS statement:
options nofmterr;
For more information, see FMTERR System Option in SAS System Options: Reference.
If SAS encounters a missing variable to format using a user-defined format and the MISSING system option defines a character to print for missing values, the missing value is determined as follows:
  • If the user-defined format or informat has a value-range-set for missing values, the missing value is defined by the user-defined format.
  • If the user-defined format does not have a value-range-set defined for missing values, the missing value is defined by the MISSING system option. The default value for the MISSING system option is . (period).

Printing Informats and Formats

The output that is provided when you use the FMTLIB option is intended to present a brief view of the informat and format values.
Instead of using the FMTLIB option, you can use the CNTLOUT= option to create an output data set that stores information about informats and formats. You can then use PROC PRINT or PROC REPORT to print the data set. In this case, labels are not truncated.
Note: You can use data set options to keep or drop references to additional variables that were added by using the CNTLOUT= option.
FOOTNOTE 1:Catalogs are a type of SAS file and reside in a SAS library. If you are unfamiliar with the types of SAS files or the SAS library structure, then see the section on SAS files in SAS Language Reference: Concepts.[return]