Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 38 ยท Data Manipulation

MOVE, CORRESPONDING and Edited Pictures

Learn COBOL's MOVE rules, group moves, MOVE CORRESPONDING, INITIALIZE and numeric edited pictures with Z, comma, period, $, asterisk, + - CR DB and B for print-ready output.

MOVE is the most-used statement in COBOL. It copies data from one item to another, converting as needed. Learning its rules removes a whole class of surprises, and combining MOVE with edited pictures turns raw stored digits into print-ready amounts such as $1,234.50.

MOVE basics

COBOL
           MOVE "SMITH" TO LAST-NAME
           MOVE 42 TO COUNTER
           MOVE PRICE TO PRINT-PRICE
           MOVE ZERO TO TOTAL
           MOVE SPACES TO ERROR-LINE
           MOVE ALL "-" TO RULER
           MOVE A TO B C D           *> one source, several receivers

The source is never changed. The rules (see also PICTURE clauses):

Move What happens
numeric to numeric Aligned on the decimal point; extra high-order digits lost, extra decimals truncated, shorter fields zero filled.
alphanumeric to alphanumeric Left-justified; padded with spaces or cut on the right.
numeric to alphanumeric Digits copied as characters (integers only; a V field loses its point).
alphanumeric to numeric Allowed only if it holds digits, and then unreliable if it does not. Test with IS NUMERIC first.
numeric to edited Formats the number with the edit symbols.

A group item is always treated as alphanumeric in a MOVE, even if all its children are numeric. Moving one group to another copies bytes: it works if the layouts match, and corrupts values if they do not.

MOVE CORRESPONDING

MOVE CORRESPONDING (CORR) moves every elementary item that has the same name in both groups, skipping the rest:

COBOL
       01 IN-REC.
          05 ID      PIC 9(5).
          05 NAME    PIC X(20).
          05 DEPT    PIC X(4).
       01 OUT-REC.
          05 NAME    PIC X(20).
          05 ID      PIC 9(5).
          05 BONUS   PIC 9(4).
COBOL
           MOVE CORRESPONDING IN-REC TO OUT-REC

ID and NAME are copied (the order in the groups does not matter); DEPT and BONUS have no match and are left alone. It saves many lines when layouts share names, but a typo in a name makes a field silently stop being copied.

INITIALIZE

INITIALIZE resets items to type-appropriate defaults: numeric items to zero, alphanumeric to spaces. On a group it processes every child, which is the tidy way to clear a record:

COBOL
           INITIALIZE OUT-REC
           INITIALIZE IN-REC REPLACING NUMERIC DATA BY 99999

Numeric edited pictures

Stored numbers are bare digits. An edited picture inserts symbols for printing. An edited field is for display only; you cannot do arithmetic with it as a source.

Symbol Effect
9 A digit, always shown
Z A digit, but a leading zero becomes a space
* A digit, but a leading zero becomes * (cheque protection)
. Real decimal point
, Inserted comma; blank if no digit before it
$ Fixed (one $) or floating (several $) currency sign
+ - Sign: + shows + or -; - shows - or a space; fixed or floating
CR DB Shown at the end only if the value is negative
B Inserted space
0 Inserted zero
/ Inserted slash (dates)
COBOL
       01 AMOUNT         PIC S9(5)V99 VALUE 1234.5.
       01 ED-1           PIC ZZ,ZZ9.99.
       01 ED-2           PIC $$,$$9.99.
       01 ED-3           PIC ***,**9.99.
       01 ED-4           PIC ZZ,ZZ9.99CR.
COBOL
           MOVE AMOUNT TO ED-1      *> " 1,234.50"
           MOVE AMOUNT TO ED-2      *> "$1,234.50"
           MOVE AMOUNT TO ED-3      *> "**1,234.50"

For an amount of -1234.5, ED-4 shows 1,234.50CR. A floating string ($$,$$9.99) puts the $ right next to the first digit; a fixed $ ($ZZ,ZZ9.99 or $99.99) stays in the first column. BLANK WHEN ZERO shows spaces for zero, and the picture editing rules require the receiving field to be big enough for the biggest value, including the editing characters.

The next block is a Python stand-in for a small subset of these rules (9, Z, *, ,, ., floating $, CR). It is not a COBOL compiler: it shows the idea of zero suppression with fixed-width output.

Python A small edited-picture formatter (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: MOVE CORRESPONDING

Python MOVE CORRESPONDING (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Moving between groups with different layouts.
  • Using an edited item as a source in arithmetic.
  • Editing pictures too short for the value (leading digits and symbols are lost).
  • Moving alphanumeric input to a numeric field without IS NUMERIC.
  • Forgetting that an edited result keeps trailing and leading spaces.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A group item that contains only numeric children is moved with MOVE. How is it treated?

    Group items are always handled as alphanumeric text in a MOVE.
  2. 2. What does PIC ZZ,ZZ9.99 show for 5.5?

    Z suppresses leading zeros as spaces, so only the 9 and the decimals are shown.
  3. 3. What does MOVE CORRESPONDING do?

    Only elementary items with matching names are moved, regardless of position.
  4. 4. Which edited picture protects a cheque amount by filling with asterisks?

    • works like Z but fills suppressed zeros with asterisks.

Summary

  • MOVE aligns numbers on the decimal point, left-justifies text, pads or truncates silently, and treats groups as text.
  • MOVE CORRESPONDING copies same-named elementary items; INITIALIZE resets by type.
  • Edited pictures (Z, *, $, ,, ., +, -, CR, DB, B, 0, /) produce print-ready text but cannot feed arithmetic.
  • Floating $, +, - sit next to the first digit; fixed ones stay in place.

Next: Arithmetic Statements