REPORT Procedure

Example 13: Storing Computed Variables as Part of a Data Set

Features:

PROC REPORT statement options: OUT=

COMPUTE statement: with a computed variable as report-item

DEFINE statement options: COMPUTED

Other features:

CHART procedure

Data set: GROCERY
Format: $SCTRFMT

Details

The report in this example does the following:
  • creates a computed variable
  • stores it in an output data set
  • uses that data set to create a chart based on the computed variable

Program That Creates the Output Data Set

libname proclib
'SAS-library';
options fmtsearch=(proclib);
title;
proc report data=grocery nowd out=profit;
   column sector manager department sales Profit;
   define profit / computed;
      /* Compute values for Profit. */
   compute profit;
      if department='np1' or department='np2' then profit=0.4*sales.sum;
      else profit=0.25*sales.sum;
   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);
Delete any existing titles.
title;
Specify the report options. The NOWD option runs PROC REPORT without the REPORT window and sends its output to the open output destinations. OUT= creates the output data set PROFIT.
proc report data=grocery nowd out=profit;
Specify the report columns. The report contains columns for Manager, Department, Sales, and Profit, which is not in the input data set. Because the purpose of this report is to generate an output data set to use in another procedure, the report layout simply uses the default usage for all the data set variables to list all the observations. DEFINE statements for the data set variables are unnecessary.
   column sector manager department sales Profit;
Define the computed column. The COMPUTED option tells PROC REPORT that Profit is defined in a compute block somewhere in the PROC REPORT step.
   define profit / computed;
Calculate the computed column. 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 values for Profit. */
   compute profit;
      if department='np1' or department='np2' then profit=0.4*sales.sum;
      else profit=0.25*sales.sum;
   endcomp;
run;

The Output Data Set

The following output is the output data set that is created by PROC REPORT. It is used as input for PROC CHART.
PROC REPORT output data set

Program That Uses the Output Data Set

options fmtsearch=(proclib);
proc chart data=profit;
   block sector / sumvar=profit;
   format sector $sctrfmt.;
   format profit dollar7.2;
   title 'Sum of Profit by Sector';
run;

Program Description

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);
Chart the data in the output data set. PROC CHART uses the output data set from the previous PROC REPORT step to chart the sum of Profit for each sector.
proc chart data=profit;
   block sector / sumvar=profit;
   format sector $sctrfmt.;
   format profit dollar7.2;
   title 'Sum of Profit by Sector';
run;

Output from Processing the Output Data Set

Sum of Profit by Sector