Associating User-Defined Formats with Variables

How SAS Finds Format Catalogs

To use the GENDER, AGEGROUP, and $COL formats in a subsequent SAS session, you must assign the libref Formtlib again.
libname formtlib 'C:UsersStudent1formatslib'; 
SAS searches for the formats GENDER, AGEGROUP, and $COL in two libraries, in this order:
  • the temporary library referenced by the libref Work
  • a permanent library referenced by the libref Formtlib
SAS uses the first instance of a specified format that it finds.
Tip
You can delete formats using PROC CATALOG.

Assigning Formats to Variables

Just as with SAS formats, you associate a user-defined format with a variable in a FORMAT statement.
data work.carsurvey;
  set cert.cars;
  format Sex gender. Age agegroup. Color $col. Income Dollar8.;
run;
Remember, you can place the FORMAT statement in either a DATA step or a PROC step. By placing the FORMAT statement in a DATA step, you permanently associate a format with a variable. Note that you do not have to specify a width value when using a user-defined format.
When you submit the PRINT procedure, the output for Work.CarSurvey now shows descriptive labels instead of the values for Age, Sex, Income, and Color.
proc print data=work.carsurvey;
run;
Output 12.1 Work.CarSuvery Data Set with Formatted Values
Data Set with Formatted Values for Age, Sex, Income, and Color
When associating a format with a variable, remember to do the following:
  • Use the same format name in the FORMAT statement that you specified in the VALUE statement.
  • Place a period at the end of the format name when it is used in the FORMAT statement.
If you do not format all of a variable's values, then those that are not listed in the VALUE statement are printed as they appear in the SAS data set. In the example below, the value of 2 was not defined in the VALUE statement for GENDER as shown in observation 3, 5, 7, and 8.
libname formtlib 'C:UsersStudent1formatslib';
proc format lib=formtlib;
   value gender 	
              1 = 'Male';
   value agegroup  
             13 -< 20  = 'Teen'
             20 -< 65  = 'Adult'
             65 - HIGH = 'Senior';
   value $col 		
             'W' = 'Moon White'
             'B' = 'Sky Blue'
             'Y' = 'Sunburst Yellow'
             'G' = 'Rain Cloud Gray';
run;
data work.carsurvey;
  set cert.cars;
  format Sex gender. Age agegroup.Color $col. Income Dollar8.;
run;
proc print data=work.carsurvey;
run;
Output 12.2 Work.Carsurvey Data Set with Missing Formatted Values
Work.Carsurvey Data Set with Missing Formatted Values

Displaying User-Defined Formats

When you build a large catalog of permanent formats, it can be easy to forget the exact spelling of a specific format name or its range of values. Adding the keyword FMTLIB to the PROC FORMAT statement displays a list of all the formats in your catalog, along with descriptions of their values.
libname formtlib 'c:sasformatslib'; 
proc format library=formtlib fmtlib; 
run;
When you submit this PROC step, a description of each format in your permanent catalog is displayed as output.
Output 12.3 Output of the Formtlib Catalog
Output of the Formtlib catalog, Format Name: AgeGroup
Output of the FmtLib catalog, Format Name: Gender
Output of the FmtLib catalog, Format Name: $Col
In addition to the name, range, and label, the format description includes the following details:
  • length of the longest label
  • number of values defined by this format
  • version of SAS that was used to create the format
  • date and time of creation
Last updated: August 23, 2018
..................Content has been hidden....................

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