Variable Labels

Names and Labels

Variables labels help us make our output more easily understood. While we use variable names when we are writing SAS programs to specify which variables are to be processed, we use variable labels in our output. Variable names are often short and cryptic, while variable labels can include spaces and special characters, allowing them to be much more expressive.

Every variable has a name. Variables may or may not have labels and formats (value labels).

Not every PROC uses labels in reporting results including modeling PROCs like GLM and GENMOD. See SAS Support for three ways around this limitation using name literals.

For example, in the data set cars, some variables have labels while others do not.

ods select variables;
proc contents data=sashelp.cars;
run;
                          The CONTENTS Procedure

                Alphabetic List of Variables and Attributes
 
       #    Variable       Type    Len    Format      Label

       9    Cylinders      Num       8                               
       5    DriveTrain     Char      5                               
       8    EngineSize     Num       8                Engine Size (L)
      10    Horsepower     Num       8                               
       7    Invoice        Num       8    DOLLAR8.                   
      15    Length         Num       8                Length (IN)    
      11    MPG_City       Num       8                MPG (City)     
      12    MPG_Highway    Num       8                MPG (Highway)  
       6    MSRP           Num       8    DOLLAR8.                   
       1    Make           Char     13                               
       2    Model          Char     40                               
       4    Origin         Char      6                               
       3    Type           Char      8                               
      13    Weight         Num       8                Weight (LBS)   
      14    Wheelbase      Num       8                Wheelbase (IN) 

Where a variable has a label, SAS uses the label in output tables and graphs. Where there is no label, SAS uses the variable name.

proc means data=sashelp.cars n mean stddev;
  var invoice enginesize;
run;
                            The MEANS Procedure

   Variable      Label                N            Mean         Std Dev
   --------------------------------------------------------------------
   Invoice                          428        30014.70        17642.12
   EngineSize    Engine Size (L)    428       3.1967290       1.1085947
   --------------------------------------------------------------------
proc sgplot data=sashelp.cars;
  scatter y=invoice x=enginesize;
  run;
The SGPlot Procedure 2 4 6 8 Engine Size (L) $0 $50,000 $100,000 $150,000 Invoice

Adding Labels

Variable labels can be added or replaced in either a DATA step or a PROC step. Added in a DATA step, the label is used in any subsequent steps that use that data set. Added in a PROC step, the label is used only in the procedure.

Labels are added with a LABEL statement, which takes the form

LABEL varname1 = "label-1" <varname2 = "label-2 ...>

(You could also use more than one LABEL statement in a step.)

For example, using the class data set, which has no labels, we can add labels to several variables. When the new version of the class data is used in the subsequent PROC MEANS, the labels are used (along with the variable names).

data class;
  set sashelp.class;
  bmi = (weight/height**2)*703;
  label age    = "Age (years)"
        height = "Height (inches)"
        bmi    = "Body Mass Index";
  run;
        
proc means data=class n mean stddev;
  var age height weight bmi;
  run;
                            The MEANS Procedure

     Variable    Label               N            Mean         Std Dev
     -----------------------------------------------------------------
     Age         Age (years)        19      13.3157895       1.4926722
     Height      Height (inches)    19      62.3368421       5.1270752
     Weight                         19     100.0263158      22.7739335
     bmi         Body Mass Index    19      17.8632519       2.0926193
     -----------------------------------------------------------------

Going back to the original, unlabeled data set, we could add labels for just one PROC step.

proc sgplot data=sashelp.class;
  hbox height / category = age;
  label age    = "Age (in years)"
        height = "Height (in inches)";
  run;
The SGPlot Procedure 50 55 60 65 70 Height (in inches) 16 15 14 13 12 11 Age (in years)

Removing Labels

To remove a variable label, simply assign the variable a null label. In other words, use the form

LABEL varname = ;

where you specify the variable name, an equals symbol, and then nothing! (The equals symbol is required.)

For example, to remove the variable label for EngineSize (“Engine Size (L)”)

proc means data=sashelp.cars;
    var enginesize cylinders;
    label enginesize = ;
run;
                            The MEANS Procedure

  Variable      N          Mean       Std Dev       Minimum       Maximum
  -----------------------------------------------------------------------
  EngineSize  428     3.1967290     1.1085947     1.3000000     8.3000000
  Cylinders   426     5.8075117     1.5584426     3.0000000    12.0000000
  -----------------------------------------------------------------------