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