Previous Topic: COMPUTE Statement

Next Topic: CONTROL Statement

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:

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:

9

Is a place holder for a numeric digit.

0

Is 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:

Data

DISPLAY Value

Printed 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

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 READS

PAGE WRITES

TOTAL PAGES

7

7

14

37

 

37

34

10

44

13

4

17

33

5

38

24

7

31

8

16

24

6

5

11

11

19

30

27

22

49