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 Library again.
libname library 'c:sasformatslib'; 
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 Library
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 carsurvey;
		infile 'Z:sasusercars.dat';
		input Age Sex Income Color $;
		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 carsurvey now shows descriptive labels instead of the values for Age, Sex, Income, and Color.
proc print data=carsurvey;
 run;
Figure 8.2 Carsurvey 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, as shown in observation 3 of the figure below.
libname library 'c:sasformatslib';
proc format lib=library;
		value gender 	
					1 = 'Male'
					2 = 'Female';
		value agegroup  
					13 -< 20  = 'Teen'
					20 -< 65  = 'Adult';
		value $col 		
					'W' = 'Moon White'
					'Y' = 'Sunburst Yellow'
					'G' = 'Rain Cloud Gray';
run; 
data carsurvey;
		infile 'Z:sasusercars.dat';
		input Age Sex Income Color $;
		format Sex gender. Age agegroup. Color $col. Income DOLLAR8.;
run;
proc print data=work.carsurvey;
run;
Figure 8.3 Work.Carsurvey Data Set
Work.Carsurvey Data Set

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 library 'c:sasformatslib'; 
proc format library=library fmtlib; 
run;
When you submit this PROC step, a description of each format in your permanent catalog is displayed as output.
Figure 8.4 SAS Output: Format Description
SAS Output: Format Description
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: January 10, 2018
..................Content has been hidden....................

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