Chapter 18: Displaying Your Data
The chapter describes how to use PROC PRINT to list all or part of a SAS data set. PROC PRINT can create decent-looking reports that include column labels and summary data. However, if you need a fancier report or want more control over its appearance, you can use PROC REPORT to produce your report. There is a tradeoff: PROC REPORT can produce more sophisticated, custom reports, but it is more complicated to use. As a matter of fact, there are entire books dedicated to PROC REPORT. For many applications, especially with the added tricks described in this chapter, you might find that the reports created by PROC PRINT will do just fine.
Producing a Simple Report Using PROC PRINT
This first example uses PROC PRINT to list the contents of one of the SASHELP data sets called Shoes. This first program does not use any options or statements—it is “bare bones.”
Program 18.1: Demonstrating PROC PRINT without any Options or Statements
title “Listing of Data Set SHOES”;
title2 “In the SASHELP Library”;
title3 “-------------------------------------------------”;
proc print data=SASHELP.Shoes;
run;
Three TITLE statements were added. You can include up to 10 TITLE statements in a program. Remember, all the TITLE statements remain in effect until you change them. If you write a new TITLE statement, say TITLE2 following the PROC PRINT statement in Program 18.1, the original second title line will be replaced by the new TITLE2 text, and all title lines higher than two will be deleted. That is, anytime you submit a new TITLEn statement, all title lines greater than n are deleted.
By default, PROC PRINT will list all variables and all observations. Also, the order of the variables will be the order that they are stored in the SAS data set. Here is the listing.
Figure 18.1: Output from Program 18.1 (Partial Listing)
The column labeled Obs is created by the procedure and it is an observation number. If you modify your data set (by sorting, adding, or deleting observations, for example), the observation number associated with a particular line of data might change. Most programmers want to replace the Obs column with a variable that identifies the observation, such as an ID or name.
Therefore, the next step is to replace the Obs column with a variable of your choice. You do this by including an ID statement. You can also select which variables to include in the listing by adding a VAR statement. Using a VAR statement also specifies the order of the columns in the report. Here is an example.
Program 18.2: Adding ID and VAR Statements to the Procedure
title “Listing of Data Set Shoes”;
title2 “In the SASHELP Library”;
title3 “-------------------------------------------------”;
proc print data=SASHELP.Shoes;
id Region;
var Product Stores Sales Inventory;
run;
An ID statement and a VAR statement were added.
The result is Program 18.2 listed below.
Here is the top portion of the revised listing.
Figure 18.2: Output from Program 18.2 (Partial Listing)
Region has replaced the Obs columns and only the variables listed in the VAR statement are included in the listing.
You can use a data set option, OBS=, to list the first n observations in the data set. This is a very useful technique when you only want to see a few observations (from a possibly very big data set) to check that your program is working OK. To see the first eight observations from data set Shoes, you can add the OBS= option to the program, like this.
Program 18.3: Using the OBS= Data Set Option to List the First Eight Observations
title “Listing of Data Set Shoes (First 8 Obs”;
title2 “In the SASHELP Library”;
title3 “-------------------------------------------------”;
proc print data=SASHELP.Shoes(obs=8);
id Region;
var Product Stores Sales Inventory;
run;
The data set option is placed in parentheses following the data set name. The listing will now show the first eight observations (shown below).
Figure 18.3: Output from Program 18.3
This listing stops at observation 8. If you want even more control over which observations to list, you can include the two data set options OBS= and FIRSTOBS=. You use FIRSTOBS= to specify the first observation to list and OBS= to specify the last observation to list. For example, to list observations 20 through 25, you would use:
proc print data=SASHELP.Shoes(firstobs=20 Obs=25);
You can use these data set options anywhere you specify a SAS data set name in a program or procedure.
Using Labels Instead of Variable Names as Column Headings
By default, PROC PRINT uses variable names, not variable labels, as column headings. If you would like variable labels to head your columns, use the LABEL procedure option. The program that follows creates a small data set (Dimensions) where several variables are given labels. You can then use PROC PRINT with and without the LABEL option to see its effect.
Program 18.4: PROC PRINT with and without a LABEL Option
data Dimensions;
input Subj $ Ht Wt Waist;
label Subj = “Subject”
Ht = “Height in Inches”
Wt = “Subject’s Weight”;
datalines;
001 68 180 35
002 75 220 40
003 60 101 28
;
title “PROC PRINT Without a LABEL Option”;
proc print data=Dimensions;
id Subj;
Var Ht Wt Waist;
run;
title “PROC PRINT with LABEL Option”;
proc print data=Dimensions label;
id Subj;
Var Ht Wt Waist;
run;
Before we look at the listings, take a look at the label for the variable Wt. Because you want to include a single quotation mark in the label, you need to enclose the label in double quotation marks. In most places where you need to enclose a string in quotation marks, either single or double quotation marks will work fine. This label is an exception. For more advanced programmers, you should also know that if you want SAS to resolve a macro variable, you need double quotation marks.
Here is the output.
Figure 18.4: Output from Program 18.4
Each listing has a use. The first one (without the LABEL option) is most useful for a programmer, and the second one (with the LABEL option) is most useful for presenting the data to a non-programmer. Note that the variable Waist was not given a label, and its name is used in the second listing.
Including a BY Variable in a Listing
You can include a BY statement with PROC PRINT to break the output down by that variable. For example, the program to produce the Health data set is reproduced here. To generate a listing broken down by Gender, you must first sort the data set by Gender and then include a BY statement with PROC PRINT. Here is the program:
Program 18.5: Listing the Health Data Set Broken Down by Gender
data Health;
infile “~/MyBookFiles/health.txt” pad;
input Subj $ 1-3
Gender $ 4
Age 5-6
HR 7-8
SBP 9-11
DBP 12-14
Chol 15-17;
run;
proc sort data=Health;
by Gender;
run;
title “Listing of Data Set Health - by Gender”;
proc print data=Health;
ID Subj;
by Gender;
run;
The output looks like this.
Figure 18.5: Output from Program 18.5
You now have separate listings for females and males.
Notice that some of the file names in the folder MyBookFiles are in lowercase and others are in Proper case. Once again, this author forgot that SAS Studio is running in a LINUX environment where file names are case sensitive and used the syntax “~/MyBookFiles/Health.txt” and received the error message “Physical file does not exist” in the log. When you see this error message, the first thing to do is check if you matched the correct file name.
Including the Number of Observations in a Listing
You can include a count of the number of observations in the entire data set or for a BY group by adding the option N=‘label’ to PROC PRINT. To add a count to a listing of the Health data set, you could use the following program.
Program 18.6: Adding a Count of the Number of Observations to the Listing
title “Listing of Data Set Health”;
proc print data=health n = “Total Observations =”;
ID Subj;
run;
By adding the N= option to PROC PRINT, you now see the total number of observations in the listing, as shown next.
Figure 18.6: Output from Program 18.6
If you use the N= option along with a BY statement, you can see the observation count for each BY group (not shown).
There are several PROC PRINT options that are not described in this chapter. You can refer to the SAS Studio Help menu to see a complete list. However, if you need even more control over the format of your report, you might need to investigate PROC REPORT. I highly recommend the following books:
Carpenter, Art. 2007. Carpenter’s Complete Guide to the PROC REPORT Procedure. Cary, NC: SAS Institute Inc.
Eslinger, Jane. 2016. The SAS Programmer’s Handbook. Cary, NC: SAS Institute Inc.
Fine, Lisa. 2013. PROC REPORT by Example: Techniques for Building Professional Reports Using SAS. Cary, NC: SAS Institute Inc.
1. Use PROC PRINT to list the first 10 observations in the SASHELP data set Fish. Replace the Obs column with the variable Species, and do not include the three variables Length1–Length3. Add three TITLE statements to produce the report heading as follows:
Listing of the First 10 Observations in Data Set Fish
Prepared by: (your name here)
-----------------------------------------------------
2. Prepare a report similar to the one in Problem 1 except break down the report by Species and include all the observations in the data set. Omit the three variables Length1–Length3 and use the PROC PRINT option NOOBS to omit the Obs column.
3. Repeat Problem 2 except use Species as a BY variable and as an ID variable. How does the listing differ from the listing in Problem 2?
4. Create a report from the SASHELP data set Retail. The report should include a title (of your choice) and show sales broken down by year. Include the number of observations for each year, and use variable labels instead of variable names as column headings. The beginning of your report should look like this: