Friday, September 4, 2009

The SUBTRACT Statement

The SUBTRACT Statement
Syntax
Format 1
SUBTRACT { identifier-1
literal-1 } ... FROM {identifier-2 [ROUNDED]} ...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-SUBTRACT]
Format 2
SUBTRACT { identifier-1
literal-1 } ... FROM { identifier-2
literal-2 } GIVING identifier-3 [ROUNDED]
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-SUBTRACT]
Format 3
SUBTRACT { CORRESPONDING
CORR } identifier-1 FROM identifier-2 [ROUNDED]
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-SUBTRACT]
Description
The SUBTRACT statement is used to subtract numeric items. Each of the threeformats works slightly differently and is described in the correspondingly numbered area.
1. The first format is used to subtract field(s) or value(s) from another field. The field(s) or value(s) listed between SUBTRACT and FROM aresummed and subtracted from the value ofthe field(s) following the FROM. The answer is stored in the individual field. For example in SUBTRACT A FROM B, the value in A is subtracted from the value in B and the result is stored in B. The value in A is unchanged. Furthermore, in SUBTRACT C D FROM E F, the sum of the values in C and D is subtracted to the value in E, storing the answer in E and the sum of C and D is also subtracted from F, storing the value in F. The values in C and D are unchanged.
2. The second format is used to subtract field(s) or value(s) from one another, storing the answer in a different field. The field(s) or value(s) listed between SUBTRACT and GIVING are added and stored in the field following thefield(s) following the GIVING. For example, in SUBTRACT A FROM B GIVING C, the value in A is subtracted to the value in B and the result is stored in C. The values in A and B are unchanged.
3. The third format is used to subtract subordinate field(s) of one group from subordinate field(s) in another, storing the answer in those fields. Those subordinate field(s) of the group item identifier-1 are subtracted from and stored in those with exactly the same name in the group item identifier-2. The names of the subordinate items must be spelled exactly the same way in both groups to qualify to participate in the subtraction.
Tips
1. The fields to be subtracted must have numeric pictures, that is, they can only have the characters 9, S, and V in their PIC clauses.
2. Receiving fields may be either numeric or numeric edited fields.
3. Use the SIZE ERROR clause to detect field overflow on the receiving field.
4. In all formats, the mathematically correct results are computed, but if the receiving field is too short in either the integer or decimal portion,the result will be truncated, the integer on the left and the decimal on the right. Including the ROUNDED phrase will result the answer field being rounded instead of truncating. Rounding is always done in the least significant portion of the answer.