6.7 - Descriptive Labels

6.7 - Descriptive Labels

There may be some cases in which your variable names would not be particularly meaningful to other people reading your reports. For example, the variables q1_08, q2_08, and q3_08 might represent, respectively, your company's sales in the first, second, and third quarters of 2008. Perhaps, then, you'd want to label q1_08 as "Sales First Quarter 2008", q2_08 as "Sales Second Quarter 2008", and so on. In order to label the columns in your report as such, you need to use:

  • a LABEL statement to assign a descriptive label to a variable, and
  • the LABEL option in the PROC PRINT statement to specify that labels, rather than variable names, be displayed.

The LABEL statement can be placed either in a DATA step or directly in the PRINT procedure. When you place the LABEL statement in a DATA step, the label gets permanently affixed to the variable and therefore is available for all subsequent procedures. That is, you permanently change the variable's label attribute. When you place the LABEL statement directly in the PRINT procedure, the label is available for use only in the PRINT procedure in which it is specified.

As a default, SAS does not print labels. You must use the LABEL option to tell it to do so. Let's take a look at a couple of examples.

Example 6.18

The following SAS program illustrates the use of the LABEL option in conjunction with the LABEL statement in the PRINT procedure:

PROC PRINT data = basic LABEL;
    label name = 'Name'
	      no_vis = 'Number of Visits'
		  type_vis = 'Type of Visit'
		  expense = 'Expense';
	id name;
	var no_vis type_vis expense;
RUN;
Name Number of Visits Type of Visit expense
Alice Smith 7 101 1001.98
Maryann White 2 101 2999.34
Thomas Jones 10 190 3904.89
Benedictine Arnold 1 190 1450.23
Felicia Ho 7 190 1209.94
John Smith 6 187 1763.09
Jane Smiley 5 190 3567.00

As you can see, the LABEL statement assigns a label to four variables — name, no_vis, type_vis, and expense. The syntax of any LABEL statement must match the syntax of the LABEL statement in this program, namely first the LABEL keyword, the variable name, an equals sign (=), and finally a descriptive label (up to 256 characters long) in quotation marks. If you forget to close the label off with a quotation mark, SAS will be sure to let you know that you have a problem with your code. The LABEL option merely tells SAS to use the assigned labels when printing the report.

Launch and run the SAS program, and review the resulting output to convince yourself the labels were printed as expected. Then, you might want to remove the LABEL option and re-run the SAS program to see that the labels that are assigned to the variables within the PRINT procedure are printed only if you also specify the LABEL option.

Example 6.19

If you look at the output from the previous program, you'll see that SAS does what it can to fit longer labels, such as Number of Visits, above the column headings. You can instead control where SAS splits long labels by using the SPLIT= option. By using the option, you tell SAS to split the labels wherever the designated split character appears. The following SAS code illustrates the PRINT procedure's SPLIT= option:

PROC PRINT data = basic SPLIT='/';
    label name = 'Name';
	label no_vis = 'Number of/Visits';
	label type_vis = 'Type of Visit';
    label expense = 'Expense';
	id name;
	var no_vis type_vis expense;
RUN;
Name Number of Visits Type of Visit Expense
Alice Smith 7 101 1001.98
Maryann White 2 101 2999.34
Thomas Jones 10 190 3904.89
Benedictine Arnold 1 190 1450.23
Felicia Ho 7 190 1209.94
John Smith 6 187 1763.09
Jane Smiley 5 190 3567.00

This program also illustrates that you can assign labels in a single LABEL statement, as we did in the previous program ... or in multiple LABEL statements, as we did here. It also illustrates that you need not specify a LABEL option in the presence of the SPLIT option... the LABEL option is implicit. The SPLIT= option tells SAS to split (no kidding?) the label defined here for the variable no_vis wherever the character '/' appears. That is, instead of printing the variable no_vis, the LABEL statement tells SAS to print a two-row heading: "Number of" on the first row and "Visits" on the second row.

Launch and run the SAS program, and review the resulting output to convince yourself the labels were printed as expected.


Legend
[1]Link
Has Tooltip/Popover
 Toggleable Visibility