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
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