REPORT Procedure

Example 9: Writing a Customized Summary on Each Page

Features:
BREAK statement options:
OL
PAGE
SUMMARIZE
COMPUTE statement arguments:
with a computed variable as report-item
BEFORE break-variable
AFTER break-variable with conditional logic
BEFORE _PAGE_

DEFINE statement options: NOPRINT

LINE statement:
pointer controls
quoted text
repeating a character string
variable values and formats
Data set: GROCERY
Formats: $SCTRFMT

$MGRFMT

$DEPTFMT

Details

The report in this example displays a record of one day's sales for each store. The rows are arranged so that all the information about one store is together, and the information for each store begins on a new page. Some variables appear in columns. Others appear only in the page heading that identifies the sector and the store's manager.
The heading that appears at the top of each page is created with the _PAGE_ argument in the COMPUTE statement.
Profit is a computed variable based on the value of Sales and Department.
The text that appears at the bottom of the page depends on the total of Sales for the store. Only the first two pages of the report appear here.

Program

libname proclib
'SAS-library';
options fmtsearch=(proclib);
proc report data=grocery nowd
            headline headskip;
   title 'Sales for Individual Stores';
   column sector manager department sales Profit;
   define sector / group noprint;
   define manager / group noprint;
   define profit / computed format=dollar11.2;
   define sales / analysis sum format=dollar11.2;
   define department / group format=$deptfmt.;
   compute profit;
      if department='np1' or department='np2'
         then profit=0.4*sales.sum;
      else profit=0.25*sales.sum;
   endcomp;
    compute before _page_ / left;
      line sector $sctrfmt. ' Sector';
      line 'Store managed by ' manager $mgrfmt.;
      line ' ';
      line ' ';
      line ' ';
   endcomp;
   break after manager / ol summarize page;
   compute after manager;
      length text $ 35;
      if sales.sum lt 500 then
         text='Sales are below the target region.';
      else if sales.sum ge 500 and sales.sum lt 1000 then
         text='Sales are in the target region.';
      else if sales.sum ge 1000 then
         text='Sales exceeded goal!';
      line ' ';
      line text $35.;
   endcomp;
   run;

Program Description

Declare the PROCLIB library. The PROCLIB library is used to store user-created formats.
libname proclib
'SAS-library';
Specify the format search library.The SAS system option FMTSEARCH= adds the SAS library PROCLIB to the search path that is used to locate formats.
options fmtsearch=(proclib);
Specify the report options. The NOWD option runs PROC REPORT without the REPORT window and sends its output to the open output destinations. NOHEADER in the PROC REPORT statement suppresses the default column headings.
proc report data=grocery nowd
            headline headskip;
Specify the title.
   title 'Sales for Individual Stores';
Specify the report columns. The report contains a column for Sector, Manager, Department, Sales, and Profit, but the NOPRINT option suppresses the printing of the columns for Sector and Manager. The page heading (created later in the program) includes their values. To get these variable values into the page heading, Sector and Manager must be in the COLUMN statement.
   column sector manager department sales Profit;
Define the group, computed, and analysis variables. In this report, Sector, Manager, and Department are group variables. Each detail row of the report consolidates the information for all observations with the same values of the group variables. Profit is a computed variable whose values are calculated in the next section of the program. FORMAT= specifies the formats to use in the report.
   define sector / group noprint;
   define manager / group noprint;
   define profit / computed format=dollar11.2;
   define sales / analysis sum format=dollar11.2;
   define department / group format=$deptfmt.;
Calculate the computed variable. Profit is computed as a percentage of Sales. For nonperishable items, the profit is 40% of the sale price. For perishable items the profit is 25%. Notice that in the compute block, you must reference the variable Sales with a compound name (Sales.sum) that identifies both the variable and the statistic that you calculate with it.
   compute profit;
      if department='np1' or department='np2'
         then profit=0.4*sales.sum;
      else profit=0.25*sales.sum;
   endcomp;
Create a customized page heading. This compute block executes at the top of each page, after PROC REPORT writes the title. It writes the page heading for the current manager's store. The LEFT option left-justifies the text in the LINE statements. Each LINE statement writes the text in quotation marks just as it appears in the statement. The first two LINE statements write a variable value with the format specified immediately after the variable's name.
    compute before _page_ / left;
      line sector $sctrfmt. ' Sector';
      line 'Store managed by ' manager $mgrfmt.;
      line ' ';
      line ' ';
      line ' ';
   endcomp;
Produce a report summary. This BREAK statement creates a default summary after the last row for each manager. OL writes a row of hyphens above the summary line. SUMMARIZE writes the value of Sales (the only analysis or computed variable) in the summary line. The PAGE option starts a new page after each default summary so that the page heading that is created in the preceding compute block always pertains to the correct manager.
   break after manager / ol summarize page;
Produce a customized summary. This compute block places conditional text in a customized summary that appears after the last detail row for each manager.
   compute after manager;
Specify the length of the customized summary text. The LENGTH statement assigns a length of 35 to the temporary variable TEXT. In this particular case, the LENGTH statement is unnecessary because the longest version appears in the first IF/THEN statement. However, using the LENGTH statement ensures that even if the order of the conditional statements changes, TEXT will be long enough to hold the longest version.
      length text $ 35;
Specify the conditional logic for the customized summary text. You cannot use the LINE statement in conditional statements (IF-THEN, IF-THEN/ELSE, and SELECT) because it does not take effect until PROC REPORT has executed all other statements in the compute block. These IF-THEN/ELSE statements assign a value to TEXT based on the value of Sales.sum in the summary row. A LINE statement writes that variable, whatever its value happens to be.
      if sales.sum lt 500 then
         text='Sales are below the target region.';
      else if sales.sum ge 500 and sales.sum lt 1000 then
         text='Sales are in the target region.';
      else if sales.sum ge 1000 then
         text='Sales exceeded goal!';
      line ' ';
      line text $35.;
   endcomp;
   run;

Output

Sales for Individual Stores