Using Custom Formats

Overview

After you have created a custom format, you can use SAS statements to permanently assign the format to a variable in a DATA step, or you can temporarily specify a format in a PROC step to determine how the data values appear in output. You should already be familiar with referencing a format in a FORMAT statement.
Another way to assign, change, or remove the format that is associated with a variable in an existing SAS data set is to use the DATASETS procedure to modify the descriptor portion of a data set.
General form, DATASETS procedure with the MODIFY and FORMAT statements:
PROC DATASETS LIB=SAS-library <NOLIST>;
MODIFY SAS-data-set;
FORMAT variable(s) format;
QUIT;
Here is an explanation of the syntax:
SAS-library
is the name of the SAS library that contains the data that you want to modify.
NOLIST
suppresses the directory listing.
SAS-data-set
is the name of the SAS data set you want to modify.
variable
is the name of one or more variables whose format you want to assign, change, or remove.
format
is the name of a format to apply to the variable or variables that are listed before it. If you do not specify a format, any format that is associated with the variable is removed.
Note: The DATASETS procedure is interactive and remains in memory until you issue the QUIT statement.

Example

In the following code, two variables in the SAS data set Flights are changed. The format $DEST. is associated with the variable Dest and the format is removed from the variable Baggage.
proc datasets lib=Mylib;
   modify flights;
   format dest $dest.;
   format baggage;
quit;

Using a Permanent Storage Location for Formats

When you permanently associate a format with a variable in a data set, it is important to ensure that the format that you are referencing is stored in a permanent location. Remember that the storage location for the format is determined when the format is created in the FORMAT procedure.
When you create formats that you want to use in subsequent SAS sessions, it is useful to take these steps:
  1. Assign the Library libref to a SAS library in the SAS session in which you are running the PROC FORMAT step.
  2. Specify LIB=LIBRARY in the PROC FORMAT step that creates the format.
  3. Include a LIBNAME statement in the program that references the format to assign the Library libref to the library that contains the permanent format catalog.
You can store formats in any catalog that you choose. However, you must identify the format catalogs to SAS before you can access them. You learn about this in a later topic.
When a format is referenced, SAS automatically looks through the following libraries in this order:
  1. Work.Formats
  2. Library.Formats
The Library libref is recommended for formats because it is automatically searched when a format is referenced. If you store formats in libraries or catalogs other than those in the default search path, you must use the FMTSEARCH= system option to tell SAS where to find your formats.
General form, FMTSEARCH= system option:
OPTIONS FMTSEARCH= (catalog-1 catalog-2...catalog-n);
Here is an explanation of the syntax:
catalog
is the name of one or more catalogs to search. The value of catalog can be either libref or libref.catalog. If only the libref is given, SAS assumes that Formats is the catalog name.
The Work.Formats catalog is always searched first, and the Library.Formats catalog is searched next, unless one or both catalogs appear in the FMTSEARCH= list.

Example

Suppose you have formats that are stored in the Rpt library and in the Prod.Newfmt catalog. The following OPTIONS statement tells SAS where to find your formats:
options fmtsearch=(rpt prod.newfmt);
Because no catalog is specified with the Rpt libref, the default catalog name Formats is assumed. This OPTIONS statement creates the following search order:
Search order
Because the Work and Library librefs were not specified in the FMTSEARCH= option, they are searched in default order.

Avoiding Format Errors

Consider what happens if you forget to specify a catalog in the FMTSEARCH= option, misspell a format name, or make some other mistake that causes SAS to fail to locate the format that you have specified.
By default, the FMTERR system option is in effect. If you use a format that SAS cannot load, SAS issues an error message and stops processing the step. To prevent this, you must change the system option FMTERR to NOFMTERR. When NOFMTERR is in effect, SAS substitutes a default format for the missing format and continues processing.
General form, FMTERR system option:
OPTIONS FMTERR | NOFMTERR;
Here is explanation of the syntax:
FMTERR
specifies that when SAS cannot find a specified variable format, it generates an error message and stops processing. Substitution of a default format does not occur.
NOFMTERR
replaces missing formats with the w. or $w. default format and continues processing.

Example

Suppose the FMTERR system option is in effect. In a previous example, we created the $ROUTES. format to group airline routes into zones. In the following code, the $ROUTES. format is misspelled:
proc print data=sasuser.cargorev(obs=10);
   format route $route.;
run;
Because FMTERR is in effect, the format cannot be located and SAS stops processing the step. An error message is written to the log.
Table 17.2 SAS Log
30   proc print data=sasuser.cargorev(obs=10);
31      format route $route.;
ERROR: The format $ROUTE was not found or could not be loaded.
32   run;


NOTE: The SAS System stopped processing this step because of errors.
If the NOFMTERR system option is specified, substitution of a default format occurs, and SAS continues to process the step.
options nofmterr;
proc print data=sasuser.cargorev(obs=10);
   format Route $route.;
run;
SAS substitutes the $w. format for the $ROUTE. format that it could not locate. No message is written to the log and processing continues. You can see from the output that the format that you intended to use has not been applied.
RevCargo
..................Content has been hidden....................

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