College of Natural Sciences
 
FAQs
This is for IE7 to hold div open

SAS FAQ #22: Variable labels in SAS

Question:

How do I generate a label for each level of a variable in SAS?

Answer:

Before the DATA step that will create your SAS dataset, use PROC FORMAT to assign labels to each level. Note that PROC FORMAT only creates informats; the informat must be associated with a variable by using the FORMAT statement in a later DATA step. Here is an example of PROC FORMAT:

PROC FORMAT ;
VALUE fnum
    1 = 'Young Males'
    2 = 'Young Females' ;
VALUE $fa
   'A' = 'Value A'
   'B' = 'Value B' ;
RUN ;

The VALUE statement assigns a name to an informat, and a label to each level of the informat. Here the first user-defined informat is named FNUM. It associates the numbers 1 and 2 with the labels Young Males and Young Females respectively. The second VALUE statement shows how to define labels for characters-valued levels; note that these levels must be enclosed in single quotes. Labels can have up to 16 characters.

The informats defined by a previous PROC FORMAT can be applied to variables in a subsequent DATA step by using the FORMAT statement. To use the FORMAT statement, follow the variable name with the appropriate informat. (Note that the informat is specified by the informat name followed by a period. The period indicates that the name is associated with an informat rather than a variable). An informat can be applied to more than one variable. All subsequent output will utilize these labels. Here is an example applying the previous PROC FORMAT informats:

DATA one ;
INPUT number letter $ ;
FORMAT number FNUM. letter $FA. ;
CARDS ;
1 A
2 B

;

It is important to realize that serious complications can result if an informat is applied which has not been defined in a previous PROC FORMAT or saved in a catalog. One way to prevent this is to include the appropriate PROC FORMATs in any program using user-defined informats.

You should not confuse value labels, shown above, with variable labels. Variable labels provide a way for you to assign a longer descriptive set of text to accompany a variable when it appears as part of SAS's output. For instance, the variable name NUMBER (shown above) may not be as descriptive as "Participant Sex". You can use the SAS LABEL statement in a SAS DATA step to assign the label with the variable name, like this:

LABEL number = 'Participant Sex';

For more information, click on the Help button in the SAS menu bar and scroll to SAS Help and Documentation.

If you have further questions, send E-mail to stats@ssc.utexas.edu.