Moving Data Between Fields
In COBOL, the MOVE statement copies data from a sending field to one or more receiving fields, converting between PICTURE representations as needed. A numeric MOVE right-justifies and zero-fills or truncates according to the receiving field's PICTURE, while an alphanumeric MOVE left-justifies and space-fills. Understanding PICTURE clauses like PIC 9(5), PIC X(10), and PIC S9(7)V99 is essential because MOVE silently reformats data to match the target, and mismatched sizes cause truncation without raising any runtime error.
Cricket analogy: Copying a batter's three-digit score of 145 into a scoreboard slot that only has room for two digits truncates it to '45', just as a numeric MOVE drops leading digits when the receiving PICTURE clause is too small, the way Sachin Tendulkar's 200 would clip on an undersized display.
MOVE CORRESPONDING and Group Moves
A group move copies an entire record or group item, including all its subordinate elementary fields, as one alphanumeric operation regardless of the underlying data types of the subordinates. This is different from MOVE CORRESPONDING, which compares the data-names of subordinate fields in the sending and receiving groups and moves only the fields whose names match exactly, skipping any that don't. MOVE CORRESPONDING is commonly used when copying between two records that share many but not all field names, such as an input record and an output record with a similar but not identical layout.
Cricket analogy: A group move is like copying an entire scorecard sheet as-is from one filing folder to another without checking individual entries, while MOVE CORRESPONDING is like transferring only the columns labeled 'Runs' and 'Wickets' between two differently formatted scorecards used by the BCCI and ICC.
01 WS-INPUT-RECORD.
05 WS-EMP-ID PIC 9(6).
05 WS-EMP-NAME PIC X(30).
05 WS-SALARY PIC 9(7)V99.
01 WS-OUTPUT-RECORD.
05 WS-EMP-ID PIC 9(6).
05 WS-EMP-NAME PIC X(30).
05 WS-DEPT-CODE PIC X(4).
PROCEDURE DIVISION.
MOVE WS-INPUT-RECORD TO WS-OUTPUT-RECORD.
*> Group move copies bytes positionally, so DEPT-CODE
*> now contains part of SALARY reinterpreted as text.
MOVE CORRESPONDING WS-INPUT-RECORD TO WS-OUTPUT-RECORD.
*> Only WS-EMP-ID and WS-EMP-NAME are moved because their
*> names match; WS-SALARY and WS-DEPT-CODE are left alone.A plain group MOVE is a byte-for-byte alphanumeric copy that ignores subordinate PICTURE clauses entirely. If the sending and receiving groups are not laid out identically, a group MOVE will silently misalign numeric fields and corrupt data without any compiler warning. Always prefer MOVE CORRESPONDING or explicit elementary-level MOVE statements when record layouts differ.
Arithmetic Verbs: ADD, SUBTRACT, MULTIPLY, and DIVIDE
COBOL provides dedicated arithmetic verbs rather than requiring an expression language for simple operations. ADD 1 TO WS-COUNTER increments a field in place, while ADD WS-A, WS-B GIVING WS-TOTAL sums two fields into a third without altering the operands. SUBTRACT, MULTIPLY, and DIVIDE follow the same GIVING pattern, and DIVIDE additionally supports REMAINDER to capture the modulus of an integer division. Every arithmetic verb can be paired with an ON SIZE ERROR clause, which executes imperative statements if the result would overflow the receiving field's PICTURE, letting the program handle the condition instead of producing corrupted data.
Cricket analogy: ADD 1 TO WS-OVERS-BOWLED after every over is like a scorer incrementing the over count on a physical scoreboard, while DIVIDE WS-RUNS BY WS-BALLS GIVING WS-STRIKE-RATE mirrors calculating a batter's strike rate, such as Virat Kohli's runs per hundred balls faced.
COMPUTE and ROUNDED
The COMPUTE statement evaluates a full arithmetic expression using standard operator precedence (parentheses, then exponentiation, then multiplication and division, then addition and subtraction) and assigns the result to one or more receiving fields, which is often clearer than chaining several individual arithmetic verbs. Adding the ROUNDED phrase to any arithmetic statement, including COMPUTE, applies standard rounding to the least significant digit instead of truncating it, which matters for financial calculations where truncation would systematically undercount fractional cents. COMPUTE is generally preferred over separate ADD/SUBTRACT/MULTIPLY/DIVIDE statements whenever an expression involves more than one operator, since it reduces the number of intermediate working-storage fields needed.
Cricket analogy: COMPUTE WS-NRR = (WS-RUNS-FOR / WS-OVERS-FOR) - (WS-RUNS-AGAINST / WS-OVERS-AGAINST) mirrors how net run rate is calculated in an IPL points table, combining multiple divisions and a subtraction in one formula rather than several separate steps.
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYROLL-CALC.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-HOURS-WORKED PIC 9(3)V99 VALUE 45.50.
01 WS-HOURLY-RATE PIC 9(3)V99 VALUE 22.75.
01 WS-OT-HOURS PIC 9(3)V99.
01 WS-GROSS-PAY PIC 9(6)V99.
PROCEDURE DIVISION.
IF WS-HOURS-WORKED > 40
SUBTRACT 40 FROM WS-HOURS-WORKED GIVING WS-OT-HOURS
COMPUTE WS-GROSS-PAY ROUNDED =
(40 * WS-HOURLY-RATE) +
(WS-OT-HOURS * WS-HOURLY-RATE * 1.5)
ON SIZE ERROR
DISPLAY "GROSS PAY OVERFLOW"
END-COMPUTE
ELSE
COMPUTE WS-GROSS-PAY ROUNDED = WS-HOURS-WORKED * WS-HOURLY-RATE
END-IF
DISPLAY "GROSS PAY: " WS-GROSS-PAY.
STOP RUN.Without ROUNDED, COBOL truncates arithmetic results to fit the receiving field's decimal places, which biases every calculation downward. For any monetary or statistical computation, always add ROUNDED unless you have a specific reason to truncate, and always pair COMPUTE or arithmetic verbs with ON SIZE ERROR when overflow is possible.
- MOVE reformats data to fit the receiving field's PICTURE clause: numeric moves right-justify and zero/truncate, alphanumeric moves left-justify and space-fill.
- A plain group MOVE copies bytes positionally regardless of subordinate field names, while MOVE CORRESPONDING only moves subordinate fields whose data-names match.
- ADD, SUBTRACT, MULTIPLY, and DIVIDE support a GIVING phrase to store results in a separate field without altering the operands.
- DIVIDE supports a REMAINDER phrase to capture the modulus of an integer division.
- ON SIZE ERROR lets a program intercept arithmetic overflow instead of silently producing a truncated or corrupted result.
- COMPUTE evaluates a full expression with standard operator precedence and is preferred over chained arithmetic verbs for multi-operator formulas.
- ROUNDED applies standard rounding instead of truncation and should be used for any monetary or precision-sensitive calculation.
Practice what you learned
1. What happens when MOVE copies a numeric value into a receiving field whose PICTURE clause has fewer digits than needed?
2. How does MOVE CORRESPONDING differ from a plain group MOVE?
3. What does the ON SIZE ERROR clause do in an arithmetic statement?
4. Why is COMPUTE often preferred over chaining several ADD, SUBTRACT, and MULTIPLY statements?
5. What is the effect of omitting ROUNDED from a COMPUTE statement involving decimal results?
Was this page helpful?
You May Also Like
Conditional Logic in COBOL
Master COBOL's IF/ELSE, nested conditions, the EVALUATE statement, and condition-names (88-levels) for expressing readable business rules.
PERFORM Statement Variations
Explore the many forms of COBOL's PERFORM statement, from simple out-of-line calls and PERFORM THRU to TIMES, UNTIL, and in-line PERFORM blocks.
Loops in COBOL
See how COBOL's PERFORM statement is used in practice to build counting loops, nested loops, sentinel-controlled read loops, and how to exit them safely.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics