Friday, September 4, 2009

The ADD Statement
Syntax
Format 1
ADD { identifier-1
literal-1 } ... TO {identifier-2 [ROUNDED]} ...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]
Format 2
ADD { identifier-1
literal-1 } ... TO { identifier-2
literal-2 } GIVING {identifier-3 [ROUNDED]}...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]
Format 3
ADD { CORRESPONDING
CORR } identifier-1 TO identifier-2 [ROUNDED]
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]

Description
The ADD statement is used to add numeric items together. Each of the three formats works slightly differently and is described in the correspondingly numbered area.
1. The first format is used to add field(s) or value(s) to another field.
The field(s) or value(s) listed between ADD and TO are summed and added to the value of the field(s) following the TO, where the answer is stored.
For example, in ADD A TO B, the value in A is added to the value in B and the result is stored in B.
The value in A is unchanged.
Furthermore, in ADD C D TO E F, the sum of the values in C and D is added to the value in E, storing the answer in E and the sum of C and D is also added to F storing the value in F. The values in C and D are unchanged.
2. The second format is used to add field(s) or value(s) to one another, storing the answer in a different field.
The field(s) or value(s) listed between ADD and GIVING are added and stored in the field following the field(s) following the GIVING.
For example, in ADD A TO B GIVING C, the value in A is added 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 add subordinate field(s) of one group to subordinate field(s) in another, storing the answer in those fields.
Those subordinate field(s) of the group item identifier-1 are added to 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 addition.
Tips
1. The fields to be added 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.