SAS Certification Prep Guide Review - 4formats
SAS Certification Prep Guide Review - 4formats
You can use a separate FORMAT statement for each variable, or you can format several
variables (using either the same format or different formats) in a single FORMAT
statement.
Figure 12.2 Specifying a Field Width (w) with the FORMAT Statement
Decimal Places
For numeric variables, you can also specify the number of decimal places (d), if any, to
be displayed in the output. Numbers are rounded to the specified number of decimal
places. In the example above, no decimal places are displayed.
Writing the whole number 2030 as 2,030.00 requires eight print positions, including two
decimal places and the decimal point.
Formatting 15374 with a dollar sign, commas, and two decimal places requires 10 print
positions.
0 MMDDYY8. 01/01/60
0 MMDDYY10. 01/01/1960
0 DATE7. 01JAN60
0 DATE9. 01JAN1960
TIP If a format is too small, the following message is written to the SAS log:
NOTE: At least one W.D format was too small for the number to be
printed. The decimal might be shifted by the 'BEST' format.
The FORMAT Procedure
Definitions
SAS format
determines how variable values are printed according to the data type: numeric,
character, date, time, or timestamp.
SAS informat
determines how data values are read and stored according to the data type: numeric,
character, date, time, or timestamp.
Anytime you use PROC FORMAT to create a format, the format is stored in a format
catalog. If the SAS library does not already contain a format catalog, SAS automatically
creates one. If you do not specify the LIBRARY= option, the formats are stored in a
default format catalog named Work.Formats.
The libref Work signifies that any format that is stored in Work.Formats is a temporary
format; it exists only for the current SAS session.
• Specify the LIBRARY= option in the PROC FORMAT statement and specify the
libref formtlib.
PROC FORMAT LIBRARY=formtlib;
The LIBRARY= option accepts a libref and a catalog in the format library.format.
When the LIBRARY= option specifies a libref and not a catalog, PROC FORMAT
uses the catalog Formats.
When you associate a user-defined format with a variable in a subsequent DATA or
PROC step, use the Library libref to reference the location of the format catalog.
Any format that you create in this PROC FORMAT step is now stored in a permanent
format catalog called Formtlib.Formats.
libname formtlib 'C:\Users\Student1\formats\lib';
proc format library=formtlib;
...more SAS statements...
run;
In the program above, the catalog Formtlib.Formats is located in the SAS library
C:\Users\Student1\formats\lib, which is referenced by the libref Formtlib.
Notice that LIB= is an acceptable abbreviation for the LIBRARY= option.
proc format lib=formtlib;
Defining a Unique Format
Notice that the statement begins with the keyword VALUE and ends with a semicolon
after all the labels have been defined. The following VALUE statements create the
GENDER, AGEGROUP, and $COL formats to specify descriptive labels that are later
assigned to the variables Sex, Age, and Color respectively:
proc format;
value gender
1 = 'Male'
2 = 'Female';
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;
When the specified values are numeric values, they are not enclosed in quotation marks,
and the format's name should not begin with a dollar sign ($).
You can also use the keywords LOW and HIGH to specify the lower and upper limits of
a variable's value range. The keyword LOW does not include missing numeric values.
The keyword OTHER can be used to label missing values as well as any values that are
not specifically addressed in a range.
proc format lib=formtlib;
value agefmt
low-<13='child'
13-<20='teenager'
20-<65='adult'
65-high='senior citizen'
other='unknown';
run;
TIP If applied to a character format, the keyword LOW includes missing character
values.
When specifying a label for displaying each range, remember to do the following:
• Enclose the label in quotation marks.
• Limit the label to 32,767 characters.
• Use two single quotation marks if you want an apostrophe to appear in the label:
000='employee''s jobtitle unknown';
To define several formats, you can use multiple VALUE statements in a single PROC
FORMAT step. In this example, each VALUE statement defines a different format.
proc format;
value gender
1 = 'Male'
2 = 'Female';
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;
The SAS log prints notes informing you that the formats have been created.
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;
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
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