FORMAT Procedure

Example 3: Creating a Format for Character Values

Features:

VALUE statement : OTHER keyword

Data set: PROCLIB.STAFF
Format: USCurrency

Details

This example uses a VALUE statement to create a character format that prints a value of a character variable as a different character string.

Program

libname proclib 'SAS-library-1';
libname library 'SAS-library-2';
   
   proc format library=library;
      value  $city 'BR1'='Birmingham UK'
                   'BR2'='Plymouth UK'
                   'BR3'='York UK'
                   'US1'='Denver USA'
                   'US2'='Miami USA'
                   other='INCORRECT CODE';
   run;
proc print data=proclib.staff noobs label;
   label salary='Salary in U.S. Dollars';
   format salary uscurrency. site $city.;
   title 'PROCLIB.STAFF with a Format for the Variables';
   title2 'Salary and Site';
run;

Program Description

Assign two SAS library references (PROCLIB and LIBRARY). Assigning a library reference LIBRARY is useful in this case because if you use PROC FORMAT, then SAS automatically searches for informats and formats in any library that is referenced with the LIBRARY libref.
libname proclib 'SAS-library-1';
libname library 'SAS-library-2';
   
Create the catalog named LIBRARY.FORMATS, where the user-defined formats will be stored. The LIBRARY= option specifies a permanent storage location for the formats that you create. It also creates a catalog named FORMAT in the specified library. If you do not use LIBRARY=, then SAS temporarily stores formats and informats that you create in a catalog named WORK.FORMATS.
   proc format library=library;
Define the $CITY. format.The special codes BR1, BR2, and so on, are converted to the names of the corresponding cities. The keyword OTHER specifies that values in the data set that do not match any of the listed city code values are converted to the value INCORRECT CODE .
      value  $city 'BR1'='Birmingham UK'
                   'BR2'='Plymouth UK'
                   'BR3'='York UK'
                   'US1'='Denver USA'
                   'US2'='Miami USA'
                   other='INCORRECT CODE';
   run;
Print the PROCLIB.STAFF data set. The NOOBS option suppresses the printing of observation numbers. The LABEL option uses variable labels instead of variable names for column headings.
proc print data=proclib.staff noobs label;
Specify a label for the Salary variable. The LABEL statement substitutes the label “Salary in U.S. Dollars” for the name SALARY.
   label salary='Salary in U.S. Dollars';
Specify formats for Salary and Site. The FORMAT statement temporarily associates the USCurrency. format with the variable SALARY and also temporarily associates the format $CITY. with the variable SITE.
   format salary uscurrency. site $city.;
Specify the titles.
   title 'PROCLIB.STAFF with a Format for the Variables';
   title2 'Salary and Site';
run;

Output

PROCLIB.STAFF with Formatted Variables for Salary and Site
PROCLIB.STAFF with Formatted Variables for Salary and Site