Chapter Contents

Previous

Next
The REPORT Procedure

How PROC REPORT Builds a Report

This section first explains the process of building a report. Following this explanation are illustrations of how PROC REPORT creates two sample reports. The examples use programming statements; you can construct the same reports in the windowing environment.

To understand the process of building a report, you must understand the difference between report variables and DATA step variables. Variables that appear only in one or more compute blocks are DATA step variables. Variables that appear in one or more columns of the report are report variables. A report variable may or may not appear in a compute block.


Sequence of Events
PROC REPORT constructs a report as follows:

  1. It consolidates the data by group, order, and across variables. It calculates all statistics for the report, those for detail rows as well as those for summary lines in breaks. Statistics include those computed for analysis variables. PROC REPORT calculates statistics for summary lines whether or not they appear in the report. It stores all this information in a temporary file.

  2. It initializes all DATA step variables to missing.

  3. It begins constructing the rows of the report.
    1. At the beginning of each row, it initializes all report variables to missing.

    2. It fills in values for report variables from left to right.

      • Values for computed variables come from executing the statements in the corresponding compute blocks.

      • Values for all other variables come from the temporary file created at the beginning of the report-building process.

    3. Whenever it comes to a break, PROC REPORT first constructs the break lines created with the BREAK or RBREAK statement or with options in the BREAK window. It then executes the statements in the compute block attached to the break (if there is one).

    Note:   Because of the way PROC REPORT builds a report, you can

    This document references these statistics with the appropriate compound name. For information about referencing report items in a compute block, see Four Ways to Reference Report Items in a Compute Block .  [cautionend]


Construction of Summary Lines

PROC REPORT constructs a summary line for a break if either of the following conditions is true:

The summary line that PROC REPORT constructs at this point is preliminary. If no compute block is attached to the break, the preliminary summary line becomes the final summary line. However, if a compute block is attached to the break, the statements in the compute block can alter the values in the preliminary summary line.

PROC REPORT prints the summary line only if you summarize numeric variables in the break.

Using Compound Names

When you use a statistic in a report, you generally refer to it in compute blocks by a compound name like Sales.sum. However, in different parts of the report, that same name has different meanings. Consider the report in Three Different Meanings of Sales.sum . The statements that create the output follow. The user-defined formats that are used are created by a PROC FORMAT step .

libname proclib 'SAS-data-library';
options nodate pageno=1 linesize=64
        pagesize=60 fmtsearch=(proclib);
proc report data=grocery nowindows;
   column sector manager sales;
   define sector / group format=$sctrfmt.;
   define sales  / analysis sum
                   format=dollar9.2;
   define manager / group format=$mgrfmt.;
   break after sector / summarize skip ol;
   rbreak after / summarize dol dul;
   compute after;
      sector='Total:';
   endcomp;
run;

Three Different Meanings of Sales.sum
                         The SAS System                        1

                 Sector     Manager      Sales
                 Northeast  Alomar     $786.00     [1]
                            Andrews  $1,045.00
                 ---------           ---------
                 Northeast           $1,831.00     [2]
                                              
                 Northwest  Brown      $598.00
                            Pelfrey    $746.00
                            Reveiz   $1,110.00
                 ---------           ---------
                 Northwest           $2,454.00
                                              
                 Southeast  Jones      $630.00
                            Smith      $350.00
                 ---------           ---------
                 Southeast             $980.00
                                              
                 Southwest  Adams      $695.00
                            Taylor     $353.00
                 ---------           ---------
                 Southwest           $1,048.00
                                              
                 =========           =========
                 Total:              $6,313.00     [3]
                 =========           =========

Here Sales.sum has three different meanings:

  1. In detail rows, the value is the sales for one manager's store in a sector of the city. For example, the first detail row of the report shows that the sales for the store that Alomar manages were $786.00.

  2. In the group summary lines, the value is the sales for all the stores in one sector. For example, the first group summary line shows that sales for the Northeast sector were $1,831.00.

  3. In the report summary line, the value ($6,313.00) is the sales for all stores in the city.

CAUTION:
When to Use an Alias Unless you use the NOALIAS option in the PROC REPORT statement, when you refer in a compute block to a statistic that has an alias, you do not use a compound name. Generally, you must use the alias. However, if the statistic shares a column with an across variable, you must reference it by column number (see Four Ways to Reference Report Items in a Compute Block ).  [cautionend]


Building a Report That Uses Groups and a Report Summary
The report in Report with Groups and a Report Summary contains five columns:

At the end of the report a break summarizes the statistics and computed variables in the report and assigns to Sector the value of TOTALS:.

The following statements produce Report with Groups and a Report Summary . The user-defined formats that are used are created by a PROC FORMAT step .

libname proclib 'SAS-data-library';
options nodate pageno=1 linesize=64
        pagesize=60 fmtsearch=(proclib);
proc report data=grocery headline headskip;
   column sector department sales Profit N;
   define sector / group format=$sctrfmt.;
   define department   / group format=$deptfmt.;
   define sales  / analysis sum
                   format=dollar9.2;
   define profit / computed format=dollar9.2;

   compute profit;
      if department='np1' or department='np2'
         then profit=0.4*sales.sum;
      else profit=0.25*sales.sum;
   endcomp;

   rbreak after / dol dul summarize;
   compute after;
      sector='TOTALS:';
   endcomp;

   where sector contains 'n';
   title 'Report for Northeast and Northwest Sectors';
run;

Report with Groups and a Report Summary
           Report for Northeast and Northwest Sectors          1

     Sector     Department      Sales     Profit          N
     ------------------------------------------------------
                                                           
     Northeast  Canned        $840.00    $336.00          2
                Meat/Dairy    $490.00    $122.50          2
                Paper         $290.00    $116.00          2
                Produce       $211.00     $52.75          2
     Northwest  Canned      $1,070.00    $428.00          3
                Meat/Dairy  $1,055.00    $263.75          3
                Paper         $150.00     $60.00          3
                Produce       $179.00     $44.75          3
     =========              =========  =========  =========
     TOTALS:                $4,285.00  $1,071.25         20
     =========              =========  =========  =========

A description of how PROC REPORT builds this report follows:

  1. PROC REPORT starts building the report by consolidating the data (Sector and Department are group variables) and by calculating the statistics (Sales.sum and N) for each detail row and for the break at the end of the report. It stores these values in a temporary file.

  2. Now, PROC REPORT is ready to start building the first row of the report. This report does not contain a break at the beginning of the report or a break before any groups, so the first row of the report is a detail row. The procedure initializes all report variables to missing, as First Detail Row with Values Initialized illustrates. Missing values for a character variable are represented by a blank, and missing values for a numeric variable are represented by a period.

    First Detail Row with Values Initialized

    [IMAGE]

  3. First Detail Row with Values Filled in from Left to Right illustrates the construction of the first three columns of the row. PROC REPORT fills in values for the row from left to right. Values come from the temporary file created at the beginning of the report-building process.

    First Detail Row with Values Filled in from Left to Right

    [IMAGE]

  4. The next column in the report contains the computed variable Profit. When it gets to this column, PROC REPORT executes the statements in the compute block that is attached to Profit. Nonperishable items (which have a value of np1 or np2) return a profit of 40%; perishable items (which have a value of p1 or p2) return a profit of 25%.
       if department='np1' or department='np2'
          then profit=0.4*sales.sum;
       else profit=0.25*sales.sum;

    The row now looks like A Computed Variable Added to the First Detail Row .

    CAUTION:
    The position of a computed variable is important. PROC REPORT assigns values to the columns in a row of a report from left to right. Consequently, you cannot base the calculation of a computed variable on any variable that appears to its right in the report.  [cautionend]

    A Computed Variable Added to the First Detail Row

    [IMAGE]

  5. Next, PROC REPORT fills in the value for the N statistic. The value comes from the temporary file created at the beginning of the report-building process. First Complete Detail Row illustrates the completed row.

    First Complete Detail Row

    [IMAGE]

  6. The procedure writes the completed row to the report.

  7. PROC REPORT repeats steps 2, 3, 4, 5, and 6 for each detail row in the report.

  8. At the break at the end of the report, PROC REPORT constructs the break lines described by the RBREAK statement. These lines include double underlining, double overlining, and a preliminary version of the summary line. The statistics for the summary line were calculated earlier (see step 1). The value for the computed variables is calculated when PROC REPORT reaches the appropriate column, just as it is in detail rows. PROC REPORT uses these values to create the preliminary version of the summary line (see Preliminary Summary Line ).

    Preliminary Summary Line

    [IMAGE]

  9. If no compute block is attached to the break, the preliminary version of the summary line is the same as the final version. However, in this example, a compute block is attached to the break. Therefore, PROC REPORT now executes the statements in that compute block. In this case, the compute block contains one statement:

       sector='TOTALS:';

    This statement replaces the value of Sector, which in the summary line is missing by default, with the word TOTALS:. After PROC REPORT executes the statement, it modifies the summary line to reflect this change to the value of Sector. The final version of the summary line appears in Final Summary Line .

    Final Summary Line

    [IMAGE]

  10. Finally, PROC REPORT writes all the break lines--underlining, overlining, and the final summary line--to the report.


Building a Report That Uses DATA Step Variables
PROC REPORT initializes report variables to missing at the beginning of each row of the report. The value for a DATA step 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 DATA step variable from the execution of one compute block to another.

Because all compute blocks share the current values of all variables, you can initialize DATA step variables at a break at the beginning of the report or at a break before a break variable. This report initializes the DATA step variable Sctrtot at a break before Sector.

CAUTION:
Timing at Breaks. PROC REPORT creates a preliminary summary line for a break before it executes the corresponding compute block. If the summary line contains computed variables, 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 you set in the compute block, you must do so explicitly in the compute block. This report illustrates this technique.

If no compute block is attached to a break, the preliminary summary line becomes the final summary line.  [cautionend]

The report in Report with DATA Step Variables contains five columns:

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.

The following statements produce Report with DATA Step Variables . The user-defined formats that are used are created by a PROC FORMAT step .

Note:   Calculations of the percentages do not multiply their results by 100 because PROC REPORT prints them with the PERCENT. format.  [cautionend]

libname proclib 'SAS-data-library';
options nodate pageno=1 linesize=64
        pagesize=60 fmtsearch=(proclib);
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;

Report with DATA Step 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:

  1. 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. It stores these values in a temporary file.

  2. PROC REPORT initializes the DATA step variable, Sctrtot, to missing (see Initialized DATA Step Variables ).

    Initialized DATA Step Variables

    [IMAGE]

  3. 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 temporary file that it created 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 Preliminary and Final Summary Line for the Break at the Beginning of the Report ).

    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.   [cautionend]

    Preliminary and Final Summary Line for the Break at the Beginning of the Report

    [IMAGE]

  4. 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 headers. (The NOHEADER option in the PROC REPORT statement suppresses the default column headers, which would have appeared before the customized summary.)

  5. 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 temporary file that it created 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 Preliminary Summary Line for the Break before the First Group of Observations ).

    Preliminary Summary Line for the Break before the First Group of Observations

    [IMAGE]

  6. 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.

    CAUTION:
    Recalculating Values in the Final Summary Line. If you do not recalculate the value for Sctrpct, it will be missing because the value of Sctrtot is missing at the time that the COMPUTE Sctrpct block executes.  [cautionend]

    Final Summary Line for the Break before the First Group of Observations

    [IMAGE]

  7. 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.

  8. Now, PROC REPORT is ready to start building the first detail row of the report. It initializes all report variables to missing. Values for DATA step variables do not change. First Detail Row with Initialized Values illustrates the first detail row at this point.

    First Detail Row with Initialized Values

    [IMAGE]

  9. Filling in Values from Left to Right 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 temporary file it created at the beginning of the report-building process.

    Filling in Values from Left to Right

    [IMAGE]

  10. 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 First Detail Row with the First Computed Variable Added .

    First Detail Row with the First Computed Variable Added

    [IMAGE]

  11. The next column in the report contains the statistic Sales.pctsum. PROC REPORT gets this value from the temporary file. The first detail row is now complete (see First Complete Detail Row ).

    First Complete Detail Row

    [IMAGE]

  12. 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.

  13. 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.

  14. 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 DATA step 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 Preliminary Summary Line for the Break before the Second Group of Observations ). The statements in the compute block for this break calculate the correct value (see the following step).

    Preliminary Summary Line for the Break before the Second Group of Observations

    [IMAGE]

    CAUTION:
    Synchronizing Values for Computed Variables in Break Lines. If the PROC REPORT step does not recalculate Sctrpct in the compute block attached to the break, 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.  [cautionend]

  15. 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.

    Final Summary Line for the Break before the Second Group of Observations

    [IMAGE]

    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.

  16. 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).


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.