Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
The COMPUTAB Procedure

Program Flow Example

This example shows how the COMPUTAB procedure processes observations in the program working storage and the COMPUTAB data table (CDT). Assume you have three years of sales and cost of goods sold (CGS) figures, and you want to determine total sales and cost of goods sold and calculate gross profit and the profit margin.

   data example;
      input year sales cgs;
      datalines;
   1988    83      52
   1989   106      85
   1990   120     114
   ;
   
   proc computab data=example;
   
      columns c88 c89 c90 total;
      rows sales cgs gprofit pctmarg;
   
      /* calculate gross profit */
      gprofit = sales - cgs;
   
      /* select a column */
      c88 = year = 1988;
      c89 = year = 1989;
      c90 = year = 1990;
   
      /* calculate row totals for sales */
      /* and cost of goods sold */
      col: total = c88 + c89 + c90;
   
      /* calculate profit margin */
      row: pctmarg = gprofit / cgs * 100;
   run;

Table 9.2 shows the CDT before any observation is read in. All the columns and rows are defined with the values initialized to 0.

Table 9.2: CDT Before any Input
  C88 C89 C90 TOTAL
SALES0000
CGS0000
GPROFIT0000
PCTMARG0000

When the first input is read in (year=1988, sales=83, and cgs=52), the input block puts the values for SALES and CGS in the C88 column since year=1988. Also the value for the gross profit for that year (GPROFIT) is calculated as indicated in the following:

      gprofit = sales-cgs;
      c88 = year = 1988;
      c89 = year = 1989;
      c90 = year = 1990;

Table 9.3 shows the CDT after the first observation is input.

Table 9.3: CDT After First Observation Input (C88=1)
  C88 C89 C90 TOTAL
SALES83000
CGS52000
GPROFIT31000
PCTMARG0000

Similarly, the second observation (year=1989, sales=106, cgs=85) is put in the second column and the GPROFIT is calculated to be 21. The third observation (year=1990, sales=120, cgs=114) is put in the third column and the GPROFIT is calculated to be 6. Table 9.4 shows the CDT after all observations are input.

Table 9.4: CDT After All Observations Input
  C88 C89 C90 TOTAL
SALES831061200
CGS52851140
GPROFIT312160
PCTMARG0000

After the input block is executed for each observation in the input data set, the first row or column block is processed. In this case, the column block is

      col: total = c88 + c89 + c90;

The column block executes for each row, calculating the TOTAL column for each row. Table 9.5 shows the CDT after the column block has executed for the first row (total=83 + 106 + 120). The total sales for the three years is 309.

Table 9.5: CDT After Column Block Executed for First Row
  C88 C89 C90 TOTAL
SALES83106120309
CGS52851140
GPROFIT312160
PCTMARG0000

Table 9.6 shows the CDT after the column block has executed for all rows and the values for total cost of goods sold and total gross profit have been calculated.

Table 9.6: CDT After Column Block Executed for All Rows
  C88 C89 C90 TOTAL
SALES83106120309
CGS5285114251
GPROFIT3121658
PCTMARG0000

Once the column block has been executed for all rows, the next block is processed. The row block is

      row: pctmarg = gprofit / cgs * 100;
The row block executes for each column, calculating the PCTMARG for each year and the total (TOTAL column) for three years. Table 9.7 shows the CDT after the row block has executed for all columns.

Table 9.7: CDT After Row Block Executed for All Columns
  C88 C89 C90 TOTAL
SALES83106120309
CGS5285114251
GPROFIT3121658
PCTMARG59.6224.715.2623.11

Chapter Contents
Chapter Contents
Previous
Previous
Next
Next
Top
Top

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