Howes-IT-Going - COBOL Program Divisions Sample Code


The identification division tells the computer the name of the program and supplies other documentation concerning the program's author, when it was written, when it was compiled, who it is intended for...etc. In fact, only the program name is required by the compiler.

     000110 PROGRAM-ID.    EXAMPLE-1-PROG.
     000120 AUTHOR.    SCHOWES.
     000140 DATE-WRITTEN.  01/01/2000.
     000150 DATE-COMPILED.
     000160 SECURITY.      LOCAL GROUP.


  • The use of full stops is important. Throughout these pages, note where they are positioned

  • The first words (PROGRAM-ID, AUTHOR etc..) are written in area A, the details are in area B

  • The DATE-COMPILED detail is written automatically by the compiler


This division tells the computer what the program will be interacting with (i.e. its environment) such as printers, disk drives, other files etc... As such, there are two important sections: the CONFIGURATION SECTION (which defines the source and object computer) and the INPUT-OUTPUT SECTION (which defines printers, files that may by used and assigns identifier names to these external features).

     000280 SOURCE-COMPUTER.   IBM PC.
     000290 OBJECT-COMPUTER.   IBM PC.
     000310 FILE-CONTROL.
     000320    SELECT INPUT-FILE ASSIGN TO 'input.dat'


  • You probably wouldn't need to bother with the configuration section (I think this is an oldie thing)
  • The DIVISION and SECTION words are written into area A but the SELECT clause should be in area B.
  • The full stop doesn't appear in the SELECT clause until after the ORGANIZATION has been specified.
  • INPUT-FILE and PRINT-FILE are user-defined names that are used in the program to refer to 'input.dat' and the printer, respectively. If the input.dat file was on a different disk drive, within a directory structure, then you could write: ...ASSIGN TO 'D:datafiles/data/input.dat'.
  • Line 000330 describes the structure or form of the data written in 'input.dat' file. In this case, each record is on a new line in the file.
  • The printer also is assigned but the organization doesn't have to be secified.
  • For the SELECT clause, if no organization is defined the computer defaults to SEQUENTIAL organization (i.e. each record appears in a long string with no line breaks.


The data division is where memory space in the computer is allocated to data and identifiers that are to be used by the program. Two important sections of this division are the FILE SECTION and the WORKING-STORAGE SECTION. The file section is used to define the structure, size and type of the data that will be read from or written to a file.

Suppose the 'input.dat' file (described above) contains a series of records about a companies customers, giving details of name, address, and customer number. If you were to open 'input.dat' with a text editor you would see each record on a new line like this:

       Joe Bloggs  20Shelly Road        Bigtown      023320
       John Dow    15Keats Avenue       Nowheresville042101
       Jock MacDoon05Elliot Drive       Midwich      100230


The different pieces of data need to be defined so that the program can read a record at a time, placing each piece of information into the right area of memory (which will be labelled by an identifier).

The file section for this may look like this:

     000400 DATA DIVISION.
     000410 FILE SECTION.
     000430 FD INPUT-FILE.
     000440 01 CUSTOMER-DATA.
     000450       03 NAME       PIC X(12).
     000460       03 ADDRESS.
     000470             05 HOUSE-NUMBER   PIC 99.
     000480             05 STREET         PIC X(19).
     000490             05 CITY           PIC X(13).
     000500       03 CUST-NUMBER PIC 9(6).


  • 'FD' stands for File Descriptor, and names the file, INPUT-FILE (assigned in the environment division), and describes the exact structure of the data in each record. All records in this file MUST be of exactly the same structure.

  • '01 CUSTOMER-DATA' is the group name and refers to all of the single record that is read into the computer memory from the file. The higher numbers (levels), 03.. and 05.. will contain the indivual fields of the record.

  • Both FD and 01 are written in area A while higher levels are in area B.

  • Level 01 is sub-grouped into level 03 fields. Notice that one of the level 03 sub-groups is itself sub-grouped into level 05. The sub-grouping could continue upwards as required to 07, 09 etc.. These numbers (except level 01) could as easily be 02, 03, 04 ...or any increasing number scale.

  • The PIC (short for PICTURE) clause indicates the size and type of data that that field contains. For example, in line 000450, the data name (identifier) NAME has been defined as holding 12 characters of alphnumeric data. It could have been written as PIC XXXXXXXXXXXX be that's a pain. 'X' means alphnumeric and can contain any ASCII character. However, even if it contained '2' you could not do any calculations on this as the information is stored as the ASCII code for the character '2', rather than the actual number 2. Line 000470 defines HOUSE-NUMBER as PIC 9(2), which can hold a 2-digit number. You can do calculations with this since '9' is used to denote a numeric field.

  • Notice how the group names (CUSTOMER-DATA and ADDRESS) do not have PIC descriptions. This is because the higher level field descriptions when added together will be the size of the group name, i.e. CUSTOMER-NUMBER will hold 46 characters which turns out to be the size of each record (spaces are included). You can refer to these group names but when doing so all data will be treated as alphanumeric and cannot be used for calculations, even if all of the higher group items are numeric.


The WORKING-STORAGE SECTION of the data division is for defining data that is to be stored in temporary memory, i.e. during program run-time. Effectively, this is where, for example, an identifier is defined that will hold the result of a calculation.

     000500 DATA DIVISION.
     000530 01 RECORD-COUNTER    PIC 9(5).

  Also see the string to be displayed on the screen is actually defined in working-storage using the VALUE clause:

     000800 01 TOTALS-IN.
     000810       03 1ST-NO   PIC 99 VALUE ZERO.
     000820       03 2ND-NO   PIC 999 VALUE 100.

The equivalent to filling an item such as 1ST-NO (above) with zeroes, is filling an alphanumeric (PIC X) item with spaces e.g. 01 MESSAGE PIC X(12) VALUE SPACES.

A further section of the data division is the LINKAGE SECTION, which is used to facilitate the sharing of data between programs (or sub-programs). See below.


The procedure division is where the logic of the program is actually found. This is where the various commands are written.

COBOL is a modular language, in that a program is usually broken up into units described as paragraphs.

     000920       PERFORM READ-DATA-FILE
     000950       STOP RUN.

The PERFORM statement is used to 'call' other paragraphs to do each task. These paragraphs would appear in the same coding and are part of the same program. In the above example, the program would consist of four paragraphs: the CONTROL-PARAGRAPH and the three called from within it. All of the paragraph names are user-defined. Even if a program only has one paragraph, it must still have a name. Regarding punctuation, as a rule there should only be two full stops in any paragraph; one after the paragraph name and the other at the end of the paragraph.



A program may also refer to a different program, called a sub-program. A sub-program is an entirely different program from the calling program, with its own divisions etc... with the exception that it does not end with STOP RUN (which would return you to the operating system), but with EXIT PROGRAM. The sub-program is a module, rather than a subroutine which is what a paragraph could be described as. The verb CALL is used to activate the sub-program:

     000800 DATA DIVISION.
     000820 01 W-DATE-IN    PIC 9(6).
     000850 LINKAGE SECTION.
     000860 01 L-DATE-IN.
     000870       03  DAY     PIC 99.
     000880       03  MONTH   PIC 99.
     000890       03  YEAR    PIC 99.
     000920       PERFORM READ-FILE
     001950       STOP RUN.

     003020 ...........
        :       ............etc.....
     004000 EXIT PROGRAM.

In the above code, a sub-program is called, named VALIDATE-DATE

In order to use data from the calling program in the sub-program the calling program uses a section in the data division called the LINKAGE SECTION. The item W-DATE-IN in the calling program occupies the same memory address as the sub-program's item L-DATE-IN, so the number placed in W-DATE-IN item using the VALUE clause is also in L-DATE-IN. Note: you cannot use VALUE in the linkage section.

The procedure division of the sub-program requiring the use of linkage section defined data must say so by: PROCEDURE DIVISION USING ...[linkage section items to be used] also refered to by the CALL ... USING. See lines 000930 and 3500 above.

In the above example, what is being called ("VALIDATE-DATE") is a literal. This means that you could use an identifier instead, allowing you a choice between sub-programs depending on what the literal had been previously defined as. For example, if a record was of type "A" then you may want to process that record using sub-program PROCESS-A-REC, but if a type "B" record the use PROCESS-B-REC.
  The logic might be as follows:

     0003000       IF RECORD-TYPE = "A" THEN 
     0003010            MOVE "PROCESS-A-REC" TO SUB-PROG
     0003020       ELSE MOVE "PROCESS-B-REC" TO SUB-PROG
     0003030       CALL SUB-PROG USING L-REC-DATA

Although I haven't described the various commands of the procedure division, the above code is fairly clear...if a marker called RECORD-TYPE has been set as "A" then place (i.e. MOVE) the string "PROCESS-A-REC" into the area of memory labelled as SUB-PROG (so now SUB-PROG contains this string). Otherwise (i.e. ELSE) it is assumed that the only other type there is can be "B" type and so "PROCESS-B-REC" is MOVEd into SUB-PROG. Depending on what the item SUB-PROG contains the desired sub-program will be called.

Sample COBOL code

                 Sample 1 Add line number program
                 Sample 2 Refresh line numbers program
                 Sample 3 Calculate prime numbers program
                 Sample 4 Create INDEXED file program
                 Sample 5 Read INDEXED file program

The sample code here was written quickly, so they aren't particularly well structured. Also, they are not the usual type of COBOL program that you would normally come across. COBOL is more likely written for business applications such as payroll programs or stock control etc... Hopefully they might give an indication of how COBOL works.

Sample 1 Add line numbers program

This program is designed to add line numbers to COBOL code that has been typed into a text editor (e.g. Notepad) in the following format:


      MOVE X TO Y
*the comment asterisk will be placed in position 7
/as will the page break solidus
      IF Y > Z THEN 
          ADD Z TO X
          MOVE X TO Z
      ELSE DISPLAY 'The hypen for continuing a string
-              'onto the next line also goes into position 7'
*all other text is placed from position 8 
*so you still need to indent where required

*lastly, there is a limit of about 
*70 characters per line (from position 8)

The text file containing COBOL code as above should be call named input.txt. Following execution, the program will produce a new file called output.cob although it will still be a simply text file, but can be compiled. The output.cob file for the above code would be:
000040     MOVE X TO Y
000050*the comment asterisk will be placed in position 7
000060/as will the page break solidus
000070     IF Y > Z THEN 
000080        ADD Z TO X
000090        MOVE X TO Z
000100     ELSE DISPLAY 'The hypen for continuing a string
000110-              'onto the next line also goes into position 7'
000120     END-IF
000130*all other text is placed from position 8 
000140*so you still need to indent where required
000150     STOP RUN.
000170*lastly, there is a limit of about 
000180*70 characters per line (from position 8)

Sample 2 Refresh line numbers program

This program is designed to refresh COBOL code line numbers following editing that would result in uneven line number increases (or even no line number at all) where lines have been inserted or deleted.

Sample 3 Calculate prime numbers program

This is a little program that calulates prime numbers. You are prompted to enter a number (up to 1999) and the program will produce a file, 'PRIME-NO.TXT, which contains a table of all prime numbers up to the value entered.

Sample 4 Create INDEXED file program

This program takes a line sequential record file and converts it to an indexed file. The records must contain a unique key field that is in strict ascending order. The input file (from a text editor) should be called 'LINESEQFILE.TXT'. The program output will be 'INDEXEDFILE.DAT'. You can change these in the ENVIRONMENT DIVISION if you want.

Sample 5 Read INDEXED file program

This program allows you to view the contents of an indexed file by generating a line sequential file of the original indexed file. If you tried to open an indexed file with a text editor you would just see gibberish. The input file for this program is 'INDEXEDFILE.DAT' giving an output text file called READFILE.TXT. Again, you can change these in the ENVIRONMENT DIVISION if you wish.

Go Back to:

This Webpage and Contents Created by Steven C. Howes © Howes-IT-Going 2000-2012 All rights reserved.