The PRINT Procedure
The PRINT Procedure
CHAPTER
27
The PRINT Procedure
Overview 783
Procedure Syntax 785
PROC PRINT Statement 786
BY Statement 791
ID Statement 792
PAGEBY Statement 794
SUM Statement 794
SUMBY Statement 795
VAR Statement 795
Results 796
Procedure Output 796
Page Layout 796
Examples 799
Example 1: Selecting Variables to Print 799
Example 2: Customizing Text in Column Headers 801
Example 3: Creating Separate Sections of a Report for Groups of Observations 803
Example 4: Summing Numeric Variables with One BY Group 805
Example 5: Summing Numeric Variables with Multiple BY Variables 808
Example 6: Limiting the Number of Sums in a Report 811
Example 7: Controlling the Layout of a Report with Many Variables 813
Example 8: Creating a Customized Layout with BY Groups and ID Variables 816
Example 9: Printing All the Data Sets in a SAS Library 819
Overview
The PRINT procedure prints the observations in a SAS data set, using all or some of
the variables. You can create a variety of reports ranging from a simple listing to a
highly customized report that groups the data and calculates totals and subtotals for
numeric variables.
Output 27.1 on page 784 illustrates the simplest kind of report that you can
produce. The statements that produce the output follow. A DATA step on page 799
creates the data set EXPREV.
options nodate pageno=1 linesize=64
pagesize=60;
Output 27.2 on page 785 is a customized report that is produced by PROC PRINT.
The statements that create this report
3 customize the title and the column headings
3 place dollar signs and commas in numeric output
3 selectively include and control the order of variables in the report
3 group the data by JobCode
3 sum the values for Salary for each job code and for all job codes.
For an explanation of the program that produces this report, see Example 8 on page
816.
The PRINT Procedure 4 Procedure Syntax 785
FA1 F $23,177.00
F $22,454.00
M $22,268.00
-------- -------------
FA1 $67,899.00
FA2 F $28,888.00
F $27,787.00
M $28,572.00
-------- -------------
FA2 $85,247.00
FA3 F $32,886.00
F $33,419.00
M $32,217.00
-------- -------------
FA3 $98,522.00
ME1 M $29,769.00
M $28,072.00
M $28,619.00
-------- -------------
ME1 $86,460.00
ME2 F $35,108.00
F $34,929.00
M $35,345.00
M $36,925.00
M $35,090.00
M $35,185.00
-------- -------------
ME2 $212,582.00
ME3 M $43,025.00
=============
$593,735.00
Procedure Syntax
Tip: Supports the Output Delivery System (see Chapter 2, “Fundamental Concepts for
Using Base SAS Procedures”)
Reminder: You can use the ATTRIB, FORMAT, LABEL, and WHERE statements. See
Chapter 3, "Statements with the Same Function in Multiple Procedures," for details.
You can also use any global statements as well. See Chapter 2, "Fundamental Concepts
for Using Base SAS Procedures," for a list.
SUMBY BY-variable;
ID variable(s);
SUM variable(s);
VAR variable(s);
Options
DATA=SAS-data-set
specifies the SAS data set to print.
Main discussion: “Input Data Sets” on page 18
DOUBLE
writes a blank line between observations.
Alias: D
Restriction: This option has no effect on the HTML output.
Featured in: Example 1 on page 799
HEADING=direction
controls the orientation of the column headings, where direction is one of the
following:
HORIZONTAL
prints all column headings horizontally.
Alias: H
VERTICAL
prints all column headings vertically.
Alias: V
Default: Headings are either all horizontal or all vertical. If you omit HEADING=,
PROC PRINT determines the direction of the column headings as follows:
3 If you do not use LABEL, spacing dictates whether column headings are
vertical or horizontal.
3 If you use LABEL and at least one variable has a label, all headings are
horizontal.
LABEL
uses variables’ labels as column headings.
Alias: L
Default: If you omit LABEL, PROC PRINT uses the variable’s name as the column
heading even if the PROC PRINT step contains a LABEL statement. If a variable
does not have a label, PROC PRINT uses the variable’s name as the column
heading.
Interaction: By default, if you specify LABEL and at least one variable has a label,
PROC PRINT prints all column headings horizontally. Therefore, using LABEL
may increase the number of pages of output. (Use HEADING=VERTICAL in the
PROC PRINT statement to print vertical column headings.)
788 PROC PRINT Statement 4 Chapter 27
with neither a BY nor a SUM statement prints the number of observations in the data set
at the end of the report and labels the number
with the value of string-1.
with a BY statement and a SUM statement prints the number of observations in the BY group
at the end of each BY group and prints the
number of observations in the data set at the end
of the report. The numbers for BY groups are
labeled with string-1; the number for the entire
data set is labeled with string-2.
ROUND
rounds unformatted numeric values to two decimal places. (Formatted values are
already rounded by the format to the specified number of decimal places.) For both
formatted and unformatted variables, PROC PRINT uses these rounded values to
calculate any sums in the report.
If you omit ROUND, PROC PRINT adds the actual values of the rows to obtain
the sum even though it displays the formatted (rounded) values. Any sums are also
rounded by the format, but they include only one rounding error, that of rounding the
sum of the actual values. The ROUND option, on the other hand, rounds values
before summing them, so there may be multiple rounding errors. The results without
ROUND are more accurate, but ROUND is useful for published reports where it is
important for the total to be the sum of the printed (rounded) values.
Be aware that the results from PROC PRINT with the ROUND option may differ
from the results of summing the same data with other methods such as PROC
MEANS or the DATA step. Consider a simple case in which
3 the data set contains three values for X: .003, .004, and .009.
3 X has a format of 5.2.
Depending on how you calculate the sum, you can get three different answers:
0.02, 0.01, and 0.016. Figure 27.1 on page 789 shows the results of calculating the
sum with PROC PRINT (without and with the ROUND option) and PROC MEANS.
Notice that the sum produced without the ROUND option (.02) is closer to the
actual result (0.16) than the sum produced with ROUND (0.01). However, the sum
produced with ROUND reflects the numbers displayed in the report.
Alias: R
CAUTION:
Do not use ROUND with PICTURE formats. ROUND is for use with numeric values.
SAS procedures treat variables that have picture formats as character variables.
Using ROUND with such variables may lead to unexpected results. 4
ROWS= page-format
formats rows on a page. Currently, PAGE is the only value that you can use for
page-format:
790 PROC PRINT Statement 4 Chapter 27
PAGE
prints only one row of variables for each observation per page. When you use
ROWS=PAGE, PROC PRINT does not divide the page into sections; it prints as
many observations as possible on each page. If the observations do not fill the last
page of the output, PROC PRINT divides the last page into sections and prints all
the variables for the last few observations.
Restriction: Physical page size does not mean the same thing in HTML output as it
does in traditional procedure output. Therefore, HTML output from PROC PRINT
appears the same whether or not you use ROWS=.
Tip: The PAGE value can reduce the number of pages in the output if the data set
contains large numbers of variables and observations. However, if the data set
contains a large number of variables but few observations, the PAGE value can
increase the number of pages in the output.
See also: “Page Layout” on page 796 for discussion of the default layout.
Featured in: Example 7 on page 813
SPLIT=’split-character’
specifies the split character, which controls line breaks in column headers. It also
uses labels as column headers. PROC PRINT breaks a column heading when it
reaches the split character and continues the header on the next line. The split
character is not part of the column heading although each occurrence of the split
character counts toward the 256-character maximum for a label.
Alias: S=
Interaction: You do not need to use both LABEL and SPLIT= because SPLIT=
implies the use of labels.
Interaction: The OBS= option honors the split character. (See the discussion of
OBS= on page 788).
Featured in: Example 2 on page 801
Note: PROC PRINT does not split labels of BY variables in the heading preceding
each BY group even if you specify SPLIT=. Instead, PROC PRINT treats the split
character as part of the label. Therefore, you probably want to avoid using a split
character when you are using the BY statement. 4
UNIFORM
See WIDTH=UNIFORM on page 791.
WIDTH=column-width
determines the column width for each variable. The value of column-width must be
one of the following:
FULL
uses a variable’s formatted width as the column width. If the variable does not
have a format that explicitly specifies a field width, PROC PRINT uses the default
width. For a character variable, the default width is the length of the variable.
For a numeric variable, the default width is 12. When you use WIDTH=FULL, the
column widths do not vary from page to page.
Tip: Using WIDTH=FULL can reduce execution time.
MINIMUM
uses for each variable the minimum column width that accommodates all values of
the variable.
Alias: MIN
The PRINT Procedure 4 BY Statement 791
UNIFORM
uses each variable’s formatted width as its column width on all pages. If the
variable does not have a format that explicitly specifies a field width, PROC
PRINT uses the widest data value as the column width. When you specify
WIDTH=UNIFORM, PROC PRINT normally needs to read the data set twice.
However, if all the variables in the data set have formats that explicitly specify a
field width (for example, BEST12. but not BEST.), PROC PRINT reads the data
set only once.
Alias: U
Tip: If the data set is large and you want a uniform report, you can save computer
resources by using formats that explicitly specify a field width so that PROC
PRINT reads the data only once.
Tip: WIDTH=UNIFORM is the same as UNIFORM.
Restriction: When not all variables have formats that explicitly specify a width,
you cannot use WIDTH=UNIFORM with an engine that supports concurrent
access if another user is updating the data set at the same time.
UNIFORMBY
formats all columns uniformly within a BY group, using each variable’s formatted
width as its column width. If the variable does not have a format that explicitly
specifies a field width, PROC PRINT uses the widest data value as the column
width.
Alias: UBY
Restriction: You cannot use UNIFORMBY with a sequential data set.
Default: If you omit WIDTH= and do not specify the UNIFORM option, PROC
PRINT individually constructs each page of output. The procedure analyzes the
data for a page and decides how best to display them. Therefore, column widths
may differ from one page to another.
Tip: Column width is affected not only by variable width but also by the length of
column headings. Long column headings may lessen the usefulness of WIDTH=.
See also: For a discussion of default column widths, see “Column Width” on page
798.
BY Statement
Produces a separate section of the report for each BY group.
“BY” on page 62
Main discussion:
Featured in: Example 3 on page 803, Example 4 on page 805, Example 5 on page 808,
Example 6 on page 811, and Example 8 on page 816
BY <DESCENDING> variable-1
< …<DESCENDING> variable-n>
<NOTSORTED>;
Required Arguments
792 ID Statement 4 Chapter 27
variable
specifies the variable that the procedure uses to form BY groups. You can specify
more than one variable. If you do not use the NOTSORTED option in the BY
statement, the observations in the data set must either be sorted by all the variables
that you specify, or they must be indexed appropriately. Variables in a BY statement
are called BY variables.
Options
DESCENDING
specifies that the data set is sorted in descending order by the variable that
immediately follows the word DESCENDING in the BY statement.
NOTSORTED
specifies that observations are not necessarily sorted in alphabetic or numeric order.
The data are grouped in another way, for example, chronological order.
The requirement for ordering or indexing observations according to the values of
BY variables is suspended for BY-group processing when you use the NOTSORTED
option. In fact, the procedure does not use an index if you specify NOTSORTED. The
procedure defines a BY group as a set of contiguous observations that have the same
values for all BY variables. If observations with the same values for the BY variables
are not contiguous, the procedure treats each contiguous set as a separate BY group.
ID Statement
Identifies observations by using the formatted values of the variables that you list instead of by
using observation numbers.
Featured in: Example 7 on page 813 and Example 8 on page 816
ID variable(s);
The PRINT Procedure 4 ID Statement 793
Required Arguments
variable(s)
specifies one or more variables to print instead of the observation number at the
beginning of each row of the report.
Restriction: If the ID variables occupy so much space that no room remains on the
line for at least one other variable, PROC PRINT writes a warning to the SAS log
and does not treat all ID variables as ID variables.
Interaction: If a variable in the ID statement also appears in the VAR statement,
the output contains two columns for that variable.
PAGEBY Statement
Controls page ejects that occur before a page is full.
Requirements: BY statement
Featured in: Example 3 on page 803
PAGEBY BY-variable;
Required Arguments
BY-variable
identifies a variable appearing in the BY statement in the PROC PRINT step. If the
value of the BY variable changes, or if the value of any BY variable that precedes it
in the BY statement changes, PROC PRINT begins printing a new page.
Interaction: If you use the BY statement with the SAS system option NOBYLINE,
which suppresses the BY line that normally appears in output produced with
BY-group processing, PROC PRINT always starts a new page for each BY group.
This behavior ensures that if you create customized BY lines by putting BY-group
information in the title and suppressing the default BY lines with NOBYLINE, the
information in the titles matches the report on the pages. (See “Creating Titles
That Contain BY-Group Information” on page 46.)
SUM Statement
Totals values of numeric variables.
Featured in: Example 4 on page 805, Example 5 on page 808, Example 6 on page 811,
and Example 8 on page 816
SUM variable(s);
Required Arguments
variable(s)
identifies the numeric variables to total in the report.
When you use a SUM statement and a BY statement with multiple BY variables,
PROC PRINT sums the SUM variables for each BY group that contains more than one
observation, just as it does if you use only one BY variable. However, it provides sums
only for those BY variables whose values change when the BY group changes. (See
Example 5 on page 808.)
Note: When the value of a BY variable changes, the SAS System considers that the
values of all variables listed after it in the BY statement also change. 4
SUMBY Statement
Limits the number of sums that appear in the report.
Requirements: BY statement
Featured in: Example 6 on page 811
SUMBY BY-variable;
Required Arguments
BY-variable
identifies a variable that appears in the BY statement in the PROC PRINT step. If
the value of the BY variable changes, or if the value of any BY variable that precedes
it in the BY statement changes, PROC PRINT prints the sums of all variables listed
in the SUM statement.
VAR Statement
Selects variables that appear in the report and determines their order.
Tip: If you omit the VAR statement, PROC PRINT prints all variables in the data set.
Featured in: Example 1 on page 799 and Example 8 on page 816
VAR variable(s);
796 Results 4 Chapter 27
Required Arguments
variable(s)
identifies the variables to print. PROC PRINT prints the variables in the order that
you list them.
Interaction: In the PROC PRINT output, variables that are listed in the ID
statement precede variables that are listed in the VAR statement. If a variable in
the ID statement also appears in the VAR statement, the output contains two
columns for that variable.
Results
Procedure Output
PROC PRINT always produces a printed report. You control the appearance of the
report with statements and options. See “Examples” on page 799 for a sampling of the
types of reports that the procedure produces.
Page Layout
By default, PROC PRINT uses an identical layout for all observations on a page of
output. First, it attempts to print observations on a single line (see Figure 27.2 on page
796).
1
Obs Var_1 Var_2 Var_3
1 ~~~~ ~~~~ ~~~~
2 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~
4 ~~~~ ~~~~ ~~~~
5 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~
If PROC PRINT cannot fit all the variables on a single line, it splits the observations
into two or more sections and prints the observation number or the ID variables at the
beginning of each line. For example, in Figure 27.3 on page 797, PROC PRINT prints
the values for the first three variables in the first section of each page and the values
for the second three variables in the second section of each page.
The PRINT Procedure 4 Page Layout 797
1
Obs Var_1 Var_2 Var_3
1 ~~~~ ~~~~ ~~~~
2 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~
2
Obs Var_4 Var_5 Var_6
Obs Var_1 Var_2 Var_3
1 ~~~~ ~~~~ ~~~~
4 ~~~~ ~~~~ ~~~~
2 ~~~~ ~~~~ ~~~~
5 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~
If PROC PRINT cannot fit all the variables on one page, the procedure prints
subsequent pages with the same observations until it has printed all the variables. For
example, in Figure 27.4 on page 797, PROC PRINT uses the first two pages to print
values for the first three observations and the second two pages to print values for the
rest of the observations.
1
Obs Var_1 Var_2 Var_3 2
1 ~~~~ ~~~~ ~~~~ Obs Var_7 Var_8 Var_9
2 ~~~~ ~~~~ ~~~~ 1 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~ 2 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~
Obs Var_4 Var_5 Var_6
1 ~~~~ ~~~~ ~~~~ Obs Var_10 Var_11 Var_12
2 ~~~~ ~~~~ ~~~~ 1 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~ 2 ~~~~ ~~~~ ~~~~
3 ~~~~ ~~~~ ~~~~
3
Obs Var_1 Var_2 Var_3 4
4 ~~~~ ~~~~ ~~~~ Obs Var_7 Var_8 Var_9
5 ~~~~ ~~~~ ~~~~ 4 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~ 5 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~
Obs Var_4 Var_5 Var_6
4 ~~~~ ~~~~ ~~~~ Obs Var_10 Var_11 Var_12
5 ~~~~ ~~~~ ~~~~ 4 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~ 5 ~~~~ ~~~~ ~~~~
6 ~~~~ ~~~~ ~~~~
Note: You can alter the page layout with the ROWS= option in the PROC PRINT
statement (see the discussion of ROWS= on page 789). 4
Note: PROC PRINT may produce slightly different output if the data set is not
RADIX addressable. Version 6 compressed files are not RADIX addressable, while
798 Page Layout 4 Chapter 27
Version 7 compressed files are RADIX addressable. (The integrity of the data is not
compromised; the procedure simply numbers the observations differently.) 4
Column Headings
By default, spacing dictates whether PROC PRINT prints column headings
horizontally or vertically. Figure 27.2 on page 796, Figure 27.3 on page 797, and Figure
27.4 on page 797 all illustrate horizontal headings. Figure 27.5 on page 798 illustrates
vertical headings.
1
V V V
a a a
O r r r
b – – –
s 1 2 3
Note: If you use LABEL and at least one variable has a label, PROC PRINT prints
all column headings horizontally unless you specify HEADING=VERTICAL. 4
Column Width
By default, PROC PRINT uses a variable’s formatted width as the column width.
(The WIDTH= option overrides this default behavior.) If the variable does not have a
format that explicitly specifies a field width, PROC PRINT uses the widest data value
for that variable on that page as the column width.
If the formatted value of a character variable or the data width of an unformatted
character variable exceeds the linesize minus the length of all the ID variables, PROC
PRINT may truncate the value. Consider the following situation:
3 The linesize is 80.
3 IdNumber is a character variable with a length of 10. It is used as an ID variable.
3 State is a character variable with a length of 2. It is used as an ID variable.
3 Comment is a character variable with a length of 200.
When PROC PRINT prints these three variables on a line, it uses 14 print positions
for the two ID variables and the space after each one. This leaves 80–14, or 66, print
positions for COMMENT. Longer values of COMMENT are truncated.
WIDTH= controls the column width.
Note: Column width is affected not only by variable width but also by the length of
column headings. Long column headings may lessen the usefulness of WIDTH=. 4
The PRINT Procedure 4 Program 799
Examples
This example
3 selects three variables for the report
3 uses variable labels as column headings
3 double spaces between rows of the report.
Program
The data set EXPREV contains information on a company’s monthly expenses and revenues for
two regions of the United States.
data exprev;
input Region $ State $ Month monyy5.
Expenses Revenues;
format month monyy5.;
datalines;
Southern GA JAN95 2000 8000
Southern GA FEB95 1200 6000
Southern FL FEB95 8500 11000
Northern NY FEB95 3000 4000
Northern NY MAR95 6000 5000
Southern FL MAR95 9800 13500
Northern MA MAR95 1500 1000
;
DOUBLE writes a blank line between observations. (This option has no effect on the HTML
output.)
proc print data=exprev double;
800 Program 4 Chapter 27
The VAR statement creates columns for Month, State, and Expenses, in that order.
var month state expenses;
Output
By default, PROC PRINT identifies each observation by number under the column heading Obs.
1 JAN95 GA 2000
2 FEB95 GA 1200
3 FEB95 FL 8500
4 FEB95 NY 3000
5 MAR95 NY 6000
6 MAR95 FL 9800
7 MAR95 MA 1500
This example
3 customizes and underlines the text in column headings for variables
customizes the column header for the column that identifies observations by
number
3 shows the number of observations in the report
3 writes the values of Expenses with commas.
Program
SPLIT= identifies the asterisk as the character that starts a new line in column headers. The N
option prints the number of observations at the end of the report. OBS= specifies the column
header for the column that identifies each observation by number. The split character (*) starts a
new line in the column heading. Therefore, the equal signs (=) in the value of OBS= underline
the column header.
The VAR statement creates columns for Month, State, and Expenses, in that order.
var month state expenses;
The LABEL statement associates a label with each variable for the duration of the PROC PRINT
step. When you use SPLIT= in the PROC PRINT statement, the procedure uses labels for column
headers. The split character (*) starts a new line in the column heading. Therefore, the equal
signs (=) in the labels underline the column headers.
label month=’Month**=====’
state=’State**=====’
expenses=’Expenses**========’;
The FORMAT statement assigns a format to use for Expenses in the report. The TITLE statement
specifies a title.
format expenses comma10.;
title ’Monthly Expenses for Offices in Each State’;
run;
Output
The PRINT Procedure 4 Program 803
1 JAN95 GA 2,000
2 FEB95 GA 1,200
3 FEB95 FL 8,500
4 FEB95 NY 3,000
5 MAR95 NY 6,000
6 MAR95 FL 9,800
7 MAR95 MA 1,500
N = 7
This example
3 suppresses the printing of observation numbers at the beginning of each row
3 presents the data for each state in a separate section of the report
3 begins a new page for each region.
Program
run;
N= prints the number of observations in a BY group at the end of that BY group. The
explanatory text that the N= option provides precedes the number. NOOBS suppresses the
printing of observation numbers at the beginning of the rows. LABEL uses variables’ labels as
column headings.
The VAR statement creates columns for Month, Expenses, and Revenues, in that order.
The BY statement produces a separate section of the report for each BY group and prints a
heading above each one. The PAGEBY statement starts a new page each time the value of Region
changes.
by region state;
pageby region;
The LABEL statement associates a label with the variable Region for the duration of the PROC
PRINT step. When you use the LABEL option in the PROC PRINT statement, the procedure uses
labels for column headings.
The FORMAT statement assigns a format to Expenses and Revenues for this report. The TITLE
statement specifies a title.
Output
This example
3 sums expenses and revenues for each region and for all regions
3 shows the number of observations in each BY group and in the whole report
3 creates a customized title, containing the name of the region. This title replaces
the default BY line for each BY group.
Program
The SAS system option NOBYLINE suppresses the printing of the default BY line. When you use
PROC PRINT with NOBYLINE, each BY group starts on a new page.
options nodate pageno=1 linesize=70 pagesize=60 nobyline;
NOOBS suppresses the printing of observation numbers at the beginning of the rows. N= prints
the number of observations in a BY group at the end of that BY group and (because of the SUM
statement) prints the number of observations in the data set at the end of the report. The first
piece of explanatory text that N= provides precedes the number for each BY group. The second
piece of explanatory text that N= provides precedes the number for the entire data set.
proc print data=exprev noobs
n=’Number of observations for the state: ’
’Number of observations for the data set: ’;
The SUM statement alone sums the values of Expenses and Revenues for the entire data set.
Because the PROC PRINT step contains a BY statement, the SUM statement also sums the
values of Expenses and Revenues for each region that contains more than one observation.
The PRINT Procedure 4 Output 807
The FORMAT statement assigns the COMMA10. format to Expenses and Revenues for this report.
The TITLE statement specifies a title. The #BYVAL specification places the current value of the
BY variable Region in the title. Because NOBYLINE is in effect, each BY group starts on a new
page, and the title serves as a BY line.
The SAS system option BYLINE resets the printing of the default BY line.
options byline;
Output
This example
3 sums expenses and revenues for
3 each region
3 each state with more than one row in the report
3 all rows in the report.
3 shows the number of observations in each BY group and in the whole report.
Program
The N option prints the number of observations in a BY group at the end of that BY group and
prints the total number of observations used in the report at the bottom of the report. NOOBS
suppresses the printing of observation numbers at the beginning of the rows.
The PRINT Procedure 4 Program 809
The BY statement produces a separate section of the report for each BY group. The SUM
statement alone sums the values of Expenses and Revenues for the entire data set. Because the
program contains a BY statement, the SUM statement also sums the values of Expenses and
Revenues for each BY group that contains more than one observation.
by region state;
sum expenses revenues;
The LABEL statement associates a label with the variable Region for the duration of the PROC
PRINT step. The BY line at the beginning of each BY group uses the label. The FORMAT
statement assigns a format to Expenses and Revenues for this report. The TITLE statement
specifies a title.
label region=’Sales Region’;
format revenues expenses comma10.;
title ’Revenue and Expense Totals for Each State and Region’;
run;
810 Output 4 Chapter 27
Output
The report uses default column headers (variable names) because neither the SPLIT= nor the
LABEL option is used. Nevertheless, the BY line at the top of each section of the report shows the
BY variables’ labels and their values. The name of a BY variable identifies the subtotals in the
report.
PROC PRINT sums Expenses and Revenues for each BY group that contains more than one
observation. However, sums are shown only for the BY variables whose values change from one
BY group to the next. For example, in the third BY group, where the sales region is Southern
and the state is FL, expenses and revenues are summed only for the state because the next BY
group is for the same region.
N = 1
N = 2
N = 2
N = 2
Total N = 7
The PRINT Procedure 4 Program 811
This example
3 creates a separate section of the report for each combination of state and region
3 sums expenses and revenues only for each region and for all regions, not for
individual states.
Program
NOOBS suppresses the printing of observation numbers at the beginning of the rows.
The SUM and BY statements work together to sum the values of Revenues and Expenses for each
BY group as well as for the whole report. The SUMBY statement limits the subtotals to one for
each region.
by region state;
sum revenues expenses;
sumby region;
812 Program 4 Chapter 27
The LABEL statement associates a label with the variable Region for the duration of the PROC
PRINT step. This label is used in the BY lines.
The FORMAT statement assigns the COMMA10. format to Expenses and Revenues for this
report. The TITLE statement specifies the title.
format revenues expenses comma10.;
title ’Revenue and Expense Figures for Each Region’;
run;
The PRINT Procedure 4 Example 7: Controlling the Layout of a Report with Many Variables 813
Output
The report uses default column headers (variable names) because neither the SPLIT= nor the
LABEL option is used. Nevertheless, the BY line at the top of each section of the report shows the
BY variables’ labels and their values. The name of a BY variable identifies the subtotals in the
report.
This example shows two ways of printing a data set with a large number of
variables: one is the default, and the other uses ROWS=. For detailed explanations of
814 Program 4 Chapter 27
the layouts of these two reports, see the ROWS= option on page 789 and see “Page
Layout” on page 796.
These reports use a pagesize of 24 and a linesize of 64 to help illustrate the different
layouts.
Note: When the two reports are written as HTML output, they do not differ. 4
Program
The data set EMPDATA contains personal and job-related information about a company’s
employees. A DATA step on page 1441 creates this data set.
data empdata;
input IdNumber $ 1-4 LastName $ 9-19 FirstName $ 20-29
City $ 30-42 State $ 43-44 /
Gender $ 1 JobCode $ 9-11 Salary 20-29 @30 Birth date7.
@43 Hired date7. HomePhone $ 54-65;
format birth hired date7.;
datalines;
1919 Adams Gerald Stamford CT
M TA2 34376 15SEP48 07JUN75 203/781-1255
1653 Alexander Susan Bridgeport CT
F ME2 35108 18OCT52 12AUG78 203/675-7715
The OBS= data set option uses only the first 12 observations to create the report. (This is just to
conserve space here.) The ID statement identifies observations with the formatted value of
IdNumber rather than with the observation number. This report is in Output 27.3 on page 815.
proc print data=empdata(obs=12);
id idnumber;
title ’Personnel Data’;
run;
ROWS=PAGE prints only one row of variables for each observation on a page. This report is in
Output 27.4 on page 816.
The PRINT Procedure 4 Output 815
Output
In the traditional procedure output, each page of this report contains values for all variables in
each observation. In the HTML output, this report is identical to the report that uses
ROWS=PAGE.
Note that PROC PRINT automatically splits the variable names that are used as column headers
at a change in capitalization if the entire name does not fit in the column. Compare, for example,
the column headers for LastName (which fits in the column) and FirstName (which does not fit
in the column).
Personnel Data 1
Id First
Number LastName Name City State Gender
Id Job
Number Code Salary Birth Hired HomePhone
Id First
Number LastName Name City State Gender
Id Job
Number Code Salary Birth Hired HomePhone
Each page of this report contains values for only some of the variables in each observation.
However, each page contains values for more observations than the default report does.
Personnel Data 1
Id First
Number LastName Name City State Gender
Id Job
Number Code Salary Birth Hired HomePhone
Program
PROC SORT creates a temporary data set in which the observations are sorted by JobCode and
Gender.
SPLIT= identifies the asterisk as the character that starts a new line in column headers.
The VAR statement and the ID statement together select the variables to include in the report.
The ID statement and the BY statement produce the special format.
id jobcode;
by jobcode;
var gender salary;
The SUM statement totals the values of Salary for each BY group and for the whole report.
sum salary;
The LABEL statement associates a label with each variable for the duration of the PROC PRINT
step. When you use SPLIT= in the PROC PRINT statement, the procedure uses labels for column
headings.
The FORMAT statement assigns a format to Salary for this report. The WHERE statement
selects for the report only the observations for job codes that contain the letters ’FA’ or ’ME’. The
TITLE statements specify two titles.
Output
The ID and BY statements work together to produce this layout. The ID variable is listed only
once for each BY group. The BY lines are suppressed. Instead, the value of the ID variable,
JobCode, identifies each BY group.
FA1 F $23,177.00
F $22,454.00
M $22,268.00
-------- -------------
FA1 $67,899.00
FA2 F $28,888.00
F $27,787.00
M $28,572.00
-------- -------------
FA2 $85,247.00
FA3 F $32,886.00
F $33,419.00
M $32,217.00
-------- -------------
FA3 $98,522.00
ME1 M $29,769.00
M $28,072.00
M $28,619.00
-------- -------------
ME1 $86,460.00
ME2 F $35,108.00
F $34,929.00
M $35,345.00
M $36,925.00
M $35,090.00
M $35,185.00
-------- -------------
ME2 $212,582.00
ME3 M $43,025.00
=============
$593,735.00
The PRINT Procedure 4 Program 819
This example prints all the data sets in a SAS library. You can use the same
programming logic with any procedure. Just replace the PROC PRINT step near the
end of the example with whatever procedure step you want to execute. The example
uses the macro language. For details about the macro language, see SAS Guide to
Macro Processing, Version 6, Second Edition.
Program
PROC DATASETS copies two data sets out of the WORK library into the PRINTLIB library to
limit the number of data sets available to the example.
The %MACRO statement creates the macro PRINTALL. When you call the macro, you can pass it
one or two parameters. The first parameter is the name of the library whose data set you want to
print. The second parameter is a library used by the macro. If you do not specify this parameter,
the WORK library is the default.
%macro printall(libname,worklib=work);
The %LOCAL statement creates two local macro variables, NUM and I, to use in a loop.
820 Program 4 Chapter 27
%local num i;
This PROC DATASETS step reads the library LIBNAME that you specify as a parameter when
you invoke the macro. The CONTENTS statement produces an output data set called TEMP1 in
WORKLIB. This data set contains an observation for each variable in each data set in the library
LIBNAME. By default, each observation includes the name of the data set that the variable is in
as well as other information about the variable. However, the KEEP= data set option writes only
the name of the data set to TEMP1.
proc datasets library=&libname memtype=data nodetails;
contents out=&worklib..temp1(keep=memname) data=_all_ noprint;
run;
This DATA step increments the value of N each time that it reads the last occurrence of a data set
name (when IF LAST.MEMNAME is true). The CALL SYMPUT statement uses the current value
of N to create a macro variable for each unique value of MEMNAME in the data set TEMP1. The
TRIM function removes extra blanks in the TITLE statement in the PROC PRINT step that
follows.
data _null_;
set &worklib..temp1 end=final;
by memname notsorted;
if last.memname;
n+1;
call symput(’ds’||left(put(n,8.)),trim(memname));
When it reads the last observation in the data set (when FINAL is true), the DATA step assigns
the value of N to the macro variable NUM. At this point in the program, the value of N is the
number of observations in the data set.
if final then call symput(’num’,put(n,8.));
The PRINT Procedure 4 Output 821
The RUN statement is crucial. It forces the DATA step to run, thus creating the macro variables
used in the CALL SYMPUT statements before the %DO loop, which uses them, executes.
run;
The %DO loop issues a PROC PRINT step for each data set. The %MEND statement ends the
macro.
%do i=1 %to #
proc print data=&libname..&&ds&i noobs;
title "Data Set &libname..&&ds&i";
run;
%end;
%mend printall;
This invocation of the PRINTALL macro prints all the data sets in the library PRINTLIB.
options nodate pageno=1 linesize=70 pagesize=60;
%printall(printlib)
Output