The COMPUTE statement performs an arithmetic operation on two fields, or one field and a constant, and creates a new field containing the result.

The COMPUTE statement is executed after any SELECT or IGNORE statements in a processing step have been executed. If you want to process a COMPUTE statement before SELECT and IGNORE processing begins, use the NOW COMPUTE statement instead.

Conditional Processing

Computed fields retain their value until they are recomputed or a value is moved to the computed field by a MOVE statement. This is especially important when a COMPUTE statement is part of an IF clause. If a condition is not met, the field is not recomputed and the value is unchanged. This may cause unexpected results when using the computed field in a PRINT TOTAL or SUMMARIZE statement.

To solve this problem

  1. Define a work field.
  2. Move '0' to the work field.
  3. Create the computed field in an IF clause.
  4. Move the value in the computed field to the work field.

You can use the work field in a PRINT TOTAL or SUMMARIZE statement. For example:

DEFINE WORKSPACE LENGTH 4 DISPLAY '999,999,999' TYPE P/D.
MOVE '0' TO WORK.
IF FLAG EQ 'ON'.
     COMPUTE SUM AS FIELD1 PLUS FIELD2 DISPLAY
     '999,999,999' TYPE P/D.
END IF.
MOVE SUM TO WORK.
PRINT TOTAL WORK.

The syntax is as follows:

COMPUTE "Parms".

Parms:

fldname1
AS fldname2
arithop
[fldname3 ‘constant’]
DISPLAY ‘dcon’
[TYPE {P/D | Z/D}]
[HD | ‘heading’ ... ]
  • fldname1
    Displays the new field name. fldname1 contains the results of the computation. fldname1 can be from one to nine characters long. It can contain alphanumeric characters (A-Z and 0-9) as well as these special characters: $ (dollar sign), ¢ (cent sign), # (pound sign), and @ (at sign). The first character cannot be a digit (0-9).
  • AS fldname2
    Specifies the name of the first field used in the compute operation.
  • arithop
    Displays the arithmetic operation to be used. Valid operations are:
    • PLUS (add)
    • MINUS (subtract)
    • TIMES (multiply)
    • OVER (divide)
  • fldname3
  • 'constant'
    Specifies the name of the second field or constant used in the operation. If this is a constant, it must be enclosed in single quotes.
  • DISPLAY 'dcon'
    Tells VMRGRW how to edit the data before it is printed. A DISPLAY value ('dcon') is required for all computed fields and must be enclosed in single quotation marks. To specify a 'dcon' value, include a place holder for each numeric digit to be printed. You can also include punctuation, such as dollar signs and commas. The following characters are allowed:
9Is a place holder for a numeric digit.
0Is a place holder that specifies that automatic zero suppression ends with the next digit.
.Is a period, which specifies the decimal point position.
-Is a minus sign, which is a sign indicator when it follows the last 9 or 0 in the 'dcon' value.

Any other characters are interpreted as punctuation.

Decimal points in fldname2 and fldname3 are aligned automatically, and an implied decimal point is placed correctly in fldname1, the result field.

The following DISPLAY values produce these printed results:

DataDISPLAY ValuePrinted Result
100408'99/99/99'10/04/08
165239'99:99:99'16:52:39
123456.78'999,999.99'123,456.78
123456.78'999,909.99'123,456.78
-123456.78'999,909.99-'123,456.78-
.78'999,909.99'0.78
  • TYPE {P/D | Z/D}
    Specifies the data type of the computed field. The types are packed decimal (P/D) or zoned decimal (Z/D). The default is P/D. The data type for fldname1 must be the same as that for fldname2 and fldname3. VMRGRW performs calculations using a packed decimal data type; it is more efficient to use the TYPE P/D default unless the fields from the input file are TYPE Z/D.
  • HD
    Specifies the printed report heading for the new field is the same as the field name. This is the default.
  • heading
    Specifies a literal printed as the report heading for the new field. Use one quoted string literal for each separate line of the column heading. Column headings are left justified for alphanumeric fields and right justified for all other data types.

Examples

  • In this example, Y2 is the product of YY times 1,000. Y2 is a 5-digit number, and the default TYPE and default column heading are used:

    COMPUTE Y2 AS YY TIMES '1000' DISPLAY '99999'.
    
  • This example uses CA VM:Account data to illustrate a VMRGRW computation:

    COMPUTE TOTLPAGES AS PAGERDS PLUS PAGEWRTS DISPLAY '999,999,999' 'TOTAL' 'PAGES'.
    

This statement creates a new field, TOTLPAGES, and a new column heading, TOTAL PAGES. TOTLPAGES is the sum of the PAGERDS and PAGEWRTS fields (as defined in the CA VM:Account data definition table). The printed result is:

PAGE READSPAGE WRITESTOTAL PAGES
7714
37 37
341044
13417
33538
24731
81624
6511
111930
272249