PROC REPORT initializes
report variables to missing at the beginning of each row of the report.
The value for a temporary variable is initialized to missing before
PROC REPORT begins to construct the rows of the report, and it remains
missing until you specifically assign a value to it. PROC REPORT retains
the value of a temporary variable from the execution of one compute
block to another.
Because all compute
blocks share the current values of all variables, you can initialize
temporary variables at a break at the beginning of the report or at
a break before a break variable. This report initializes the temporary
variable Sctrtot at a break before Sector.
Note: PROC REPORT creates a preliminary
summary line for a break before it executes the corresponding compute
block. If the summary line contains computed variables, then the computations
are based on the values of the contributing variables in the preliminary
summary line. If you want to recalculate computed variables based
on values that you set in the compute block, then you must do so explicitly
in the compute block. This report illustrates this technique. If no
compute block is attached to a break, then the preliminary summary
line becomes the final summary line.
The report in
Report with Temporary Variables contains five columns:
-
Sector and Department are group
variables.
-
Sales is an analysis variable that
is used twice in this report: once to calculate the Sum statistic,
and once to calculate the Pctsum statistic.
-
Sctrpct is a computed variable
whose values are based on the values of Sales and a temporary variable,
Sctrtot, which is the total sales for a sector.
At the beginning of
the report, a customized report summary tells what the sales for all
stores are. At a break before each group of observations for a department,
a default summary summarizes the data for that sector. At the end
of each group a break inserts a blank line.
Note: Calculations of the percentages
do not multiply their results by 100 because PROC REPORT prints them
with the PERCENT. format.
libname proclib
'SAS-library';
options nodate pageno=1 linesize=64
pagesize=60 fmtsearch=(proclib);
ods html close;
ods listing;
proc report data=grocery noheader nowindows;
column sector department sales
Sctrpct sales=Salespct;
define sector / 'Sector' group
format=$sctrfmt.;
define department / group format=$deptfmt.;
define sales / analysis sum
format=dollar9.2 ;
define sctrpct / computed
format=percent9.2 ;
define salespct / pctsum format=percent9.2;
compute before;
line ' ';
line @16 'Total for all stores is '
sales.sum dollar9.2;
line ' ';
line @29 'Sum of' @40 'Percent'
@51 'Percent of';
line @6 'Sector' @17 'Department'
@29 'Sales'
@40 'of Sector' @51 'All Stores';
line @6 55*'=';
line ' ';
endcomp;
break before sector / summarize ul;
compute before sector;
sctrtot=sales.sum;
sctrpct=sales.sum/sctrtot;
endcomp;
compute sctrpct;
sctrpct=sales.sum/sctrtot;
endcomp;
break after sector/skip;
where sector contains 'n';
title 'Report for Northeast and Northwest Sectors';
run;
ods listing close;
Report with Temporary Variables
Report for Northeast and Northwest Sectors 1
Total for all stores is $4,285.00
Sum of Percent Percent of
Sector Department Sales of Sector All Stores
=======================================================
Northeast $1,831.00 100.00% 42.73%
--------- --------- --------- ---------
Northeast Canned $840.00 45.88% 19.60%
Meat/Dairy $490.00 26.76% 11.44%
Paper $290.00 15.84% 6.77%
Produce $211.00 11.52% 4.92%
Northwest $2,454.00 100.00% 57.27%
--------- --------- --------- ---------
Northwest Canned $1,070.00 43.60% 24.97%
Meat/Dairy $1,055.00 42.99% 24.62%
Paper $150.00 6.11% 3.50%
Produce $179.00 7.29% 4.18%
A description of how
PROC REPORT builds this report follows:
-
PROC REPORT starts building
the report by consolidating the data (Sector and Department are group
variables) and by calculating the statistics (Sales.sum and Sales.pctsum)
for each detail row, for the break at the beginning of the report,
for the breaks before each group, and for the breaks after each group.
-
PROC REPORT initializes
the temporary variable,
Sctrtot,
to missing. (See the following figure.)
-
Because this PROC REPORT
step contains a COMPUTE BEFORE statement, the procedure constructs
a preliminary summary line for the break at the beginning of the report.
This preliminary summary line contains values for the statistics (Sales.sum
and Sales.pctsum) and the computed variable
(Sctrpct).
At this break, Sales.sum
is the sales for all stores, and Sales.pctsum is the percentage those
sales represent for all stores (100%). PROC REPORT takes the values
for these statistics from the statistics that were computed at the
beginning of the report-building process.
The value for Sctrpct
comes from executing the statements in the corresponding compute block.
Because the value of Sctrtot is missing, PROC REPORT cannot calculate
a value for Sctrpct. Therefore, in the preliminary summary line (which
is not printed in this case), this variable also has a missing value.
(See the following figure.)
The statements in the
COMPUTE BEFORE block do not alter any variables. Therefore, the final
summary line is the same as the preliminary summary line.
Note: The COMPUTE BEFORE statement
creates a break at the beginning of the report. You do not need to
use an RBREAK statement.
-
Because the program
does not include an RBREAK statement with the SUMMARIZE option, PROC
REPORT does not write the final summary line to the report. Instead,
it uses LINE statements to write a customized summary that embeds
the value of Sales.sum into a sentence and to write customized column
headings. (The NOHEADER option in the PROC REPORT statement suppresses
the default column headings, which would have appeared before the
customized summary.)
-
Next, PROC REPORT constructs
a preliminary summary line for the break before the first group of
observations. (This break both uses the SUMMARIZE option in the BREAK
statement and has a compute block attached to it. Either of these
conditions generates a summary line.) The preliminary summary line
contains values for the break variable (Sector), the statistics (Sales.sum
and Sales.pctsum), and the computed variable (Sctrpct). At this break,
Sales.sum is the sales for one sector (the northeast sector). PROC
REPORT takes the values for Sector, Sales.sum, and Sales.pctsum from
the statistics that were computed at the beginning of the report-building
process.
The value for Sctrpct
comes from executing the statements in the corresponding compute blocks.
Because the value of Sctrtot is still missing, PROC REPORT cannot
calculate a value for Sctrpct. Therefore, in the preliminary summary
line, Sctrpct has a missing value. (See the following figure.)
-
PROC REPORT creates
the final version of the summary line by executing the statements
in the COMPUTE BEFORE SECTOR compute block. These statements execute
once each time the value of Sector changes.
-
The first statement assigns the
value of Sales.sum, which in that part of the report represents total
sales for one Sector, to the variable Sctrtot.
-
The second statement completes
the summary line by recalculating Sctrpct from the new value of Sctrtot.
The following figure shows the final summary line.
Note: In this example, you must
recalculate the value for Sctrpct in the final summary line. If you
do not recalculate the value for Sctrpct, then it will be missing
because the value of Sctrtot is missing at the time that the COMPUTE
Sctrpct block executes.
-
Because the program
contains a BREAK BEFORE statement with the SUMMARIZE option, PROC
REPORT writes the final summary line to the report. The UL option
in the BREAK statement underlines the summary line.
-
Now, PROC REPORT is
ready to start building the first report row. It initializes all report
variables to missing. Values for temporary variables do not change.
The following figure illustrates the first detail row at this point.
-
The following figure
illustrates the construction of the first three columns of the row.
PROC REPORT fills in values for the row from left to right. The values
come from the statistics that were computed at the beginning of the
report-building process.
-
The next column in the
report contains the computed variable Sctrpct. When it gets to this
column, PROC REPORT executes the statement in the compute block attached
to Sctrpct. This statement calculates the percentage of the sector's
total sales that this department accounts for:
sctrpct=sales.sum/sctrtot;
The row now looks like
the following figure.
-
The next column in the
report contains the statistic Sales.pctsum. PROC REPORT gets this
value from the statistics that are created at the beginning of the
report-building process. The first detail row is now complete. (See
the following figure.)
-
PROC REPORT writes the
detail row to the report. It repeats steps 8, 9, 10, 11, and 12 for
each detail row in the group.
-
After writing the last
detail row in the group to the report, PROC REPORT constructs the
default group summary. Because no compute block is attached to this
break and because the BREAK AFTER statement does not include the SUMMARIZE
option, PROC REPORT does not construct a summary line. The only action
at this break is that the SKIP option in the BREAK AFTER statement
writes a blank line after the last detail row of the group.
-
Now the value of the
break variable changes from
Northeast
to
Northwest
.
PROC REPORT constructs a preliminary summary line for the break before
this group of observations. As at the beginning of any row, PROC REPORT
initializes all report variables to missing but retains the value
of the temporary variable. Next, it completes the preliminary summary
line with the appropriate values for the break variable (Sector),
the statistics (Sales.sum and Sales.pctsum), and the computed variable
(Sctrpct). At this break, Sales.sum is the sales for the Northwest
sector. Because the COMPUTE BEFORE Sector block has not yet executed,
the value of Sctrtot is still $1,831.00, the value for the Northeast
sector. Thus, the value that PROC REPORT calculates for Sctrpct in
this preliminary summary line is incorrect. (See the following figure.)
The statements in the compute block for this break calculate the correct
value. (See the following step.)
CAUTION:
Synchronize
values for computed variables in break lines to prevent incorrect
results.
If the PROC REPORT
step does not recalculate Sctrpct in the compute block that is attached
to the break, then the value in the final summary line will not be
synchronized with the other values in the summary line, and the report
will be incorrect.
-
PROC REPORT creates
the final version of the summary line by executing the statements
in the COMPUTE BEFORE Sector compute block. These statements execute
once each time the value of Sector changes.
-
The first statement assigns the
value of Sales.sum, which in that part of the report represents sales
for the Northwest sector, to the variable Sctrtot.
-
The second statement completes
the summary line by recalculating Sctrpct from the new, appropriate
value of Sctrtot. The following figure shows the final summary line.
Because the program
contains a BREAK BEFORE statement with the SUMMARIZE option, PROC
REPORT writes the final summary line to the report. The UL option
in the BREAK statement underlines the summary line.
-
Now, PROC REPORT is
ready to start building the first row for this group of observations.
It repeats steps 8 through 16 until it has processed all observations
in the input data set (stopping with step 14 for the last group of
observations).