FORMAT Procedure

Example 4: Writing a Format for Dates Using a Standard SAS Format

Features:

VALUE statement:: HIGH keyword

Data set: PROCLIB.STAFF
Formats: USCurrency.

$City.

Details

This example uses an existing format that is supplied by SAS as a formatted value.
Tasks include the following:
  • creating a numeric format
  • nesting formats
  • writing a format using a standard SAS format
  • formatting dates

Program

libname proclib 'SAS-library-1';
libname library 'SAS-library-2';
proc format library=library;
   value benefit
      low-'31DEC1999'd=[worddate20.]
      '01JAN2000'd-high='  ** Not Eligible **';
   run;
proc print data=proclib.staff noobs label;
   label salary='Salary in U.S. Dollars';
   format salary uscurrency. site $city. hiredate benefit.;
   title 'PROCLIB.STAFF with a Format for the Variables';
   title2 'Salary, Site, and HireDate';
run;

Program Description

This program defines a format called BENEFIT, which differentiates between employees hired on or before 31DEC1999. The purpose of this program is to indicate any employees who are eligible to receive a benefit, based on a hire date on or before December 31, 1999. All other employees with a later hire date are listed as ineligible for the benefit.
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';
Store the BENEFIT. format in the catalog LIBRARY.FORMATS. The LIBRARY= option specifies the permanent storage location LIBRARY for the formats that you create. 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 first range in the BENEFIT. format. This first range differentiates between the employees who were hired on or before 31DEC1999 and those who were hired after that date. The keyword LOW and the SAS date constant '31DEC1999'D create the first range, which includes all date values that occur on or before December 31, 1999. For values that fall into this range, SAS applies the WORDDATEw. format. For more information about SAS date constants, see Dates, Times, and Intervals in SAS Language Reference: Concepts.For more information about the WORDDATE formats, see WORDDATEw. Format in SAS Formats and Informats: Reference.
   value benefit
      low-'31DEC1999'd=[worddate20.]
      '01JAN2000'd-high='  ** Not Eligible **';
   run;
Print the data set PROCLIB.STAFF. 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, Site, and Hiredate. The FORMAT statement associates the USCurrency. format (created in Creating a Picture Format) with SALARY, the $CITY. format (created in Creating a Format for Character Values) with SITE, and the BENEFIT. format with HIREDATE.
   format salary uscurrency. site $city. hiredate benefit.;
Specify the titles.
   title 'PROCLIB.STAFF with a Format for the Variables';
   title2 'Salary, Site, and HireDate';
run;

Output

PROCLIB.STAFF with a Format for the Variables Salary, Site, and HireDate
PROCLIB.STAFF with a Format for the Variables Salary, Site, and HireDate