The COMPUTAB Procedure

Program Flow Example

(View the complete code for this 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 figures for sales and cost of goods sold (CGS), and you want to determine total sales and cost of goods sold and calculate gross profit and the profit margin.


title 'Program Flow Example for PROC COMPUTAB';

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 3 shows the CDT before any observation is read in. All the columns and rows are defined with the values initialized to 0.

Table 3: CDT before Any Input

C88C89C90TOTAL
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 statements:

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

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

Table 4: CDT after First Observation Input (C88=1)

C88C89C90TOTAL
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 5 shows the CDT after all observations are input.

Table 5: CDT after All Observations Input

C88C89C90TOTAL
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 6 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 6: CDT after Column Block Executed for First Row

C88C89C90TOTAL
SALES83106120309
CGS52851140
GPROFIT312160
PCTMARG0000


Table 7 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 7: CDT after Column Block Executed for All Rows

C88C89C90TOTAL
SALES83106120309
CGS5285114251
GPROFIT3121658
PCTMARG0000


After 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 8 shows the CDT after the row block has executed for all columns.

Table 8: CDT after Row Block Executed for All Columns

C88C89C90TOTAL
SALES83106120309
CGS5285114251
GPROFIT3121658
PCTMARG59.6224.715.2623.11


Last updated: August 11, 2020