When you have a report item that is a grouping/ordering variable, you can specify the COMPUTE statement with both the
BEFORE/ AFTER and the report item. This allows you to execute the compute block when values of the grouping
variable change. The processing opportunities are roughly similar to FIRST. and LAST. processing in the DATA step. The
following COMPUTE statement sets up a compute block that will be executed just before each new value of REGION.
compute before region;
It is not at all unusual to have compute blocks that execute both BEFORE and AFTER in the same REPORT step.
Compute blocks that execute before the report or group are very useful for initializing variables, while compute blocks that
execute after the report or group are more generally used for summaries.
Using a compute block with a BEFORE or AFTER does not require a corresponding BREAK or RBREAK statement.
However when both are present in the same step the compute block and its corresponding BREAK or RBREAK statement
share a common row in the computed summary information. Remember that the compute block itself does not transfer
information to the report (unless it contains a LINE statement). The compute block can be used to modify information on
the computed summary information, and if there is a corresponding BREAK/RBREAK statement with a SUMMARIZE
option then that information can also be written to the final report.
CREATING AND MODIFYING COLUMNS
One of the great strengths of PROC REPORT is its ability to create columns based on calculations carried out during the
execution of the procedure. Columns that are not on the incoming data set can be created and displayed within the PROC
REPORT step. Often this can eliminate one or more DATA steps. The compute block provides the power and flexibility to
mold the column in a variety of ways.
Compute blocks can be used to create both numeric and character variables, and an extensive number of the SAS
language elements that give the DATA step much of its power and functionality are also available for use in the compute
Coordinating with the COLUMN and DEFINE Statements
Variables on the data set that is to be processed by PROC REPORT are named on the COLUMN statement and the way
that these variables are to be used is specified on the DEFINE statement. This is also true for report item columns
created through the use of the compute block. In addition, the name of the computed variable will also appear on the
When a column is created through a compute block, the new column is named and that column name is used on both the
COLUMN statement and on a DEFINE statement. Since a new column with have a define usage of COMPUTE, it cannot
also be a grouping or order variable, therefore the BEFORE/AFTER locations cannot be specified on the COMPUTE
statement. Since the name of the computed report item will appear on the COLUMN, DEFINE, and COMPUTE
statements, it is this name that is used to create and coordinate the link between the three statements.
In the following example the patients weights are to be displayed in both pounds and kilograms (the earlier example
converts to kilograms without creating a new column). Since the units for the variable WT are in pounds, a new column
containing the weight in kilograms will need to be created. This is accomplished in a compute block which creates the
computed variable WTKG.
* Creating a new column with a compute block;
title1 'Using The COMPUTE Block';
title2 'Adding a Computed Column';
proc report data=rptdata.clinics nowd split='*';
column lname sex (' Weight *--' wt wtkg); ì
define lname / order width=18 'Last Name*--';
define sex / display width=6 'Gender*--';
define wt / display format=6. 'Pounds*--';
define wtkg í / computed î format=9.2 'Kilograms*--';
compute wtkg; ï
wtkg = wt / 2.2; ð
Beyond the Basics
SAS Global Forum 2008