10.8 - Using Computed Variables

We have just one more type of variable usage, namely that of computed variables, to discuss. Computed variables are numeric or character variables that you define for the report. They are not in your input data set, nor does the REPORT procedure add them to the input data set. That is, they exist only for the purpose of displaying them in your report.

Here's how we'll go about adding a computed variable to our reports:

  1. Specify the computed variable name 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.

Let's take a look at an example!

Example 10.18 Section

The following REPORT procedure uses the Slope and USGA scores to compute a Bogey rating for each golf course that appears in the stat480.penngolf data set:

PROC REPORT data = stat480.penngolf NOWINDOWS HEADLINE;
     title 'Some Pennsylvania Golf Courses';
     column Name Slope USGA Bogey;
	 define Bogey / computed 'Bogey/Rating' format = 7.3;
	 define USGA / format = 4.1 spacing = 5;
	 compute Bogey;
	   Bogey = 0.186*Slope.sum + USGA.sum;
	 endcomp;
RUN;

Some Pennsylvania Golf Courses

Name

Slope

USGA

Bogey Rating

Toftrees

134

74.3

99.224

Penn State Blue

128

72.0

95.808

Centre Hills

128

71.2

95.008

Lewistown CC

125

72.3

95.550

State College Elks

123

70.9

93.778

Park Hills CC

126

69.3

92.736

Sinking Valley CC

132

73.4

97.952

Williamsport CC

131

71.9

96.266

Standing Stone CC

120

71.4

93.720

Bucknell GC

132

70.0

94.552

Mount Airy Lodge

140

74.3

100.340

Let's review the procedure. The COLUMN statement tells SAS that we'd like four columns to appear in our report, namely the currently existing Name, Slope, and USGA variables and the yet-to-be-computed Bogey variable. The first DEFINE statement tells SAS that Bogey is a computed variable, and specifies how to format the result and label the column. The second DEFINE statement merely tells SAS how to format and space the USGA column. Then, comes the compute block.

As you can see, the compute block begins with a COMPUTE statement containing the name of the variable to be computed, namely Bogey. Then, we use a basic assignment statement to calculate the value of Bogey. Well, okay, it doesn't look quite the same as an assignment statement that you'd expect to see in a DATA statement. Here, we have to use a compound name that identifies both the original variable and the statistic that the REPORT procedure now calculates from it. In general, the compound name has the form variable-name.statistic.  This is why we refer to Slope.sum and USGA.sum. Finally, we close the compute block with an ENDCOMP statement.

Now, let's have you go ahead and launch and run  the SAS program, and review the output so that you can convince yourself that Bogey was calculated as described.

Incidentally, the position of a computed variable in the COLUMN statement is critically important. The REPORT procedure assigns values to the columns in a row of a report from left to right. Therefore, you can't base the calculation of a computed variable that appears to its right in the report. To convince yourself of this, move the Bogey variable to the first position in the COLUMN statement, and re-run  the SAS program. Doesn't work now, does it?