SAS OnlineTutor HomeFAQ PageSuggested Learning PathContents+Searchback||next

Creating Enhanced List and Summary Reports
Defining Variable Usage


Using Computed Variables

The last type of variable usage is reserved for computed variables, which are numeric or character variables that you define for the report. They are not in the input data set, and PROC REPORT doesn't add them to the input data set. You can't change the usage of a computed variable.

In the nonwindowing environment, you add a computed variable as follows:

  1. Include the computed variable in the COLUMN statement.
  2. Define the variable's usage as COMPUTED in the DEFINE statement.
  3. Compute the value of the variable in a compute block that is associated with the variable.
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 can't base the calculation of a computed variable on any variable that appears to its right in the report.

Let's see how you create a new variable for your report.


Example

Suppose you want to determine the number of empty seats for each flight. To do so, you can compute the variable EmptySeats by subtracting the number of passengers deplaning (Deplaned) from the plane's total seats (Capacity).

In the following program, you

  • specify EmptySeats in the COLUMN statement to the right of the variables that are used in its calculation.

  • define EmptySeats as a computed variable in a DEFINE statement.

  • begin a compute block by specifying EmptySeats in a COMPUTE statement.

  • use DATA step statements in the compute block to calculate EmptySeats' value. Notice that when you refer to an analysis variable, you use a compound name that identifies both the original variable and the statistic that PROC REPORT now calculates from it. The compound name has the form variable-name.statistic.

  • close the compute block with an ENDCOMP statement.

     proc report data=flights.europe nowd;
        where dest in ('LON','PAR');
        column flight capacity deplaned emptyseats;
        define flight / width=6;
        define emptyseats / computed 'Empty Seats';
        compute emptyseats;
           emptyseats=capacity.sum-deplaned.sum;
        endcomp;
     run;

The program creates the following output.


Partial PROC REPORT Output
Flight Capacity Deplaned Empty Seats
821 250 222 28
271 250 163 87
271 250 227 23
821 250 222 28
821 250 158 92


back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement