In this topic, we described about the below sections -
The SUBTRACT statement subtracts one numeric item or the sum of two or more numeric items from one or more numeric items. The SUBTRACT stores the result into the numeric item immediately followed after FROM statement or into a separate numeric item.
Syntax-1: SUBTRACT statement
All identifiers or literals preceding the keyword FROM are added together, the sum is subtracted from identifier-2 and stored immediately in identifier-2.
The same process is repeated for each successive occurrence of identifier-2 in the left-to-right order where the identifier-2 is specified at left.
Syntax-2: SUBTRACT statement with GIVING phrase
All identifiers or literals preceding the keyword FROM are added together and their sum is subtracted from identifier-2 or literal-2. The subtraction result is stored as a new value of each data item referenced by identifier-3.
Syntax-3: SUBTRACT statement with CORRESPONDING phrase
Elementary data items within identifier-1 are subtracted from corresponding elementary items of identifier-2 and the results are stored in the corresponding elementary data items of identifier-2.
Specifies an elementary numeric data item in syntax-1. Specifies an elementary numeric data item if the identifier preceded GIVING word. Each identifier following the word GIVING must name a numeric or numeric-edited elementary data item. Specifies an alphanumeric group item or a national group item.
Rules to date fields -
- In syntax-1, if identifier-1 specifies a date field, then every instance of identifier-2 must specify a date field that is compatible with the date field specified by identifier-1. If identifier-1 does not specify a date field, then identifier-2 can specify one or more date fields with no restriction on their format.
- In syntax-2, if identifier-1 specifies a date field, then the FROM identifier-2 must be a date field that is compatible with the date field specified by identifier-1. identifier-3 can specify one or more date fields. If identifier-2 specifies a date field and identifier-1 does not, then every instance of identifier-3 must specify a date field that is compatible with the date field identifier-2.
- In syntax-3, if an item within identifier-1 is a date field, then the corresponding item within identifier-2 must be a compatible date field.
Must be a numeric literal. Floating-point data items and literals can be used where numeric data items and literals can be specified.
ROUNDED phrase -
ROUNDED option used to round the fraction result of the compute statement exceeds the length of the target data item fractional places. After decimal point alignment, the number of fraction places in arithmetic operation is compared with the number of fraction places of result identifier.
If the size of fractional result exceeds the number of factional digits declared in resultant identifier, truncation occurs and the excess fractional digits ignored.
If the ROUNDED option specified, the least significant digit of the resultant identifier is increased by 1 if the next fraction value greater than 5.
In a floating-point arithmetic operation, the result of a floating-point operation is always rounded and the ROUNDED phrase has no effect.
SIZE ERROR phrase -
A size error condition can occur in four different ways:
- When the result value of an arithmetic expression, exceeds the largest value that can be contained in the result field.
- When the year of the arithmetic statement result falls outside the century window.
The size error condition applies to results, not to any intermediate results. If the ROUNDED phrase is specified, rounding takes place before size error checking. When a size error occurs, the subsequent action of the program depends on the action specified in ON SIZE ERROR phrase.
If the ON SIZE ERROR phrase is not specified and a size error condition occurs, truncation rules applies and the resultant stores in the identifier.
If the ON SIZE ERROR phrase is specified and a size error condition occurs, the resultant value affected by the size error is not altered; i.e., the error results are not placed in the receiving identifier. After completion of the execution of the arithmetic operation, the imperative-statements in the ON SIZE ERROR phrase is executed, control is transferred to the end of the arithmetic statement.
If an individual arithmetic operation causes a size error condition (SUBTRACT CORRESPONDING statements), the ON SIZE ERROR imperative statement is not executed until all the individual subtractions have been completed.
If the NOT ON SIZE ERROR phrase has been specified and a size error condition does not exist, the NOT ON SIZE ERROR phrase is executed.
CORRESPONDING phrase -
The CORRESPONDING (CORR) phrase come up with ADD, SUBTRACT and MOVE operations. The CORRESPONDING phase used to perform the above operations on same named elementary data items from two different group items.
Both identifiers that follow the CORRESPONDING keyword must be group items. identifier-1 references the sending group item. identifier-2 references the receiving group item. identifier-1, identifier-2 or both can be subordinate to a FILLER item.
- In an ADD statement, both data items are elementary numeric data items. Other data items are ignored.
- The two elementary items should have the same name and the same qualifiers.
- The elementary items are not identified by the keyword FILLER.
- Neither identifier-1 nor identifier-2 is defined as a level 66, 77 or 88 items.
- Neither identifier-1 nor identifier-2 can be reference-modified.
- The elementary items do not include a REDEFINES, RENAMES or OCCURS clause in their definitions. However, identifier-1 and identifier-2 themselves can contain a REDEFINES or OCCURS clause in their definition.
END-SUBTRACT phrase -
The END-SUBTRACT used to end the scope of the SUBTRACT statement. END-SUBTRACT is not required when SUBTRACT statement ended with period. END-SUBTRACT can also be used with an imperative SUBTRACT statement.END-SUBTRACT permits conditional SUBTRACT to be nested in another conditional statement.