top of page

A very short COBOL program (http://www.csis.ul.ie/cobol/examples/default.htm)

$ SET SOURCEFORMAT"FREE"

IDENTIFICATION DIVISION.

PROGRAM-ID.  ShortestProgram.

 

PROCEDURE DIVISION.

DisplayPrompt.

    DISPLAY "I did it".

    STOP RUN.

Display a Message (https://www.tutorialspoint.com/cobol/cobol_tutorial.pdf)

IDENTIFICATION DIVISION.

PROGRAM-ID. HELLO.

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

SELECT FILEN ASSIGN TO INPUT.

ORGANIZATION IS SEQUENTIAL.

ACCESS IS SEQUENTIAL.

DATA DIVISION.

FILE SECTION.

FD FILEN 01 NAME PIC A(25).

WORKING-STORAGE SECTION.

01 WS-STUDENT PIC A(30).

01 WS-ID PIC 9(5).

LOCAL-STORAGE SECTION.

01 LS-CLASS PIC 9(3).

LINKAGE SECTION. 01 LS-ID PIC 9(5).

PROCEDURE DIVISION.

DISPLAY 'Executing COBOL program using JCL'.

STOP RUN.

 

The JCL to execute the above COBOL program is as follows:

 //SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C

//STEP1 EXEC PGM=HELLO

//INPUT DD DSN=ABC.EFG.XYZ,DISP=SHR

 

Output

Executing COBOL program using JCL

Demonstrating Conditions (http://www.csis.ul.ie/cobol/examples/Conditn/Conditions.htm)

 

 

      $ SET SOURCEFORMAT"FREE"

IDENTIFICATION DIVISION.

PROGRAM-ID.  Conditions.

AUTHOR.  Michael Coughlan.

* An example program demonstrating the use of

* condition names (level 88's).

* The EVALUATE and PERFORM verbs are also used.

 

DATA DIVISION.

WORKING-STORAGE SECTION.

01  Char               PIC X.

    88 Vowel           VALUE "a", "e", "i", "o", "u".

    88 Consonant       VALUE "b", "c", "d", "f", "g", "h"

                             "j" THRU "n", "p" THRU "t", "v" THRU "z".

    88 Digit           VALUE "0" THRU "9".

    88 ValidCharacter  VALUE "a" THRU "z", "0" THRU "9".

 

PROCEDURE DIVISION.

Begin.

    DISPLAY "Enter lower case character or digit. No data ends.".

    ACCEPT Char.

    PERFORM UNTIL NOT ValidCharacter

        EVALUATE TRUE

           WHEN Vowel DISPLAY "The letter " Char " is a vowel."

           WHEN Consonant DISPLAY "The letter " Char " is a consonant."

           WHEN Digit DISPLAY Char " is a digit."

           WHEN OTHER DISPLAY "problems found"

        END-EVALUATE

    END-PERFORM

    STOP RUN.

Reads a sequential file and displays the record (http://www.csis.ul.ie/cobol/examples/SeqRead/Seqread.cbl)

     $ SET SOURCEFORMAT"FREE"

IDENTIFICATION DIVISION.

PROGRAM-ID.  SeqRead.

AUTHOR.  Michael Coughlan.

* An example program showing how to read a sequential file.

* This is the definitive example

 

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

    SELECT StudentFile ASSIGN TO "STUDENTS.DAT"

               ORGANIZATION IS LINE SEQUENTIAL.

 

DATA DIVISION.

FILE SECTION.

FD StudentFile.

01 StudentDetails.

   88  EndOfStudentFile  VALUE HIGH-VALUES.

   02  StudentId        PIC 9(7).

   02  StudentName.

       03 Surname      PIC X(8).

       03 Initials          PIC XX.

   02  DateOfBirth.

       03 YOBirth         PIC 9(4).

       03 MOBirth        PIC 9(2).

       03 DOBirth        PIC 9(2).

   02  CourseCode    PIC X(4).

   02  Gender            PIC X.

 

PROCEDURE DIVISION.

Begin.

   OPEN INPUT StudentFile

   READ StudentFile

      AT END SET EndOfStudentFile TO TRUE

   END-READ

   PERFORM UNTIL EndOfStudentFile

      DISPLAY StudentId SPACE StudentName SPACE CourseCode SPACE YOBirth

      READ StudentFile

         AT END SET EndOfStudentFile TO TRUE

      END-READ

   END-PERFORM

   CLOSE StudentFile

   STOP RUN.

Counts the number of students born each month

(http://www.csis.ul.ie/cobol/examples/Tables/MonthTable.htm

 

   $ SET SOURCEFORMAT"FREE"

IDENTIFICATION DIVISION.

PROGRAM-ID.  MonthTable.

AUTHOR.  Michael Coughlan.

* This program counts the number of students born in each month and

* displays the result.

 

ENVIRONMENT DIVISION.

INPUT-OUTPUT SECTION.

FILE-CONTROL.

    SELECT StudentFile ASSIGN TO "STUDENTS.DAT"

               ORGANIZATION IS LINE SEQUENTIAL.

 

DATA DIVISION.

FILE SECTION.

FD StudentFile.

01 StudentDetails.

   88  EndOfStudentFile  VALUE HIGH-VALUES.

   02  StudentId       PIC 9(7).

   02  StudentName.

       03 Surname      PIC X(8).

       03 Initials     PIC XX.

   02  DateOfBirth.

       03 YOBirth      PIC 9(4).

       03 MOBirth      PIC 9(2).

       03 DOBirth      PIC 9(2).

   02  CourseCode      PIC X(4).

   02  Gender          PIC X.

 

WORKING-STORAGE SECTION.

01 MonthTable.

   02 TableValues.

      03 FILLER       PIC X(18) VALUE "January  February".

      03 FILLER       PIC X(18) VALUE "March    April".

      03 FILLER       PIC X(18) VALUE "May      June".

      03 FILLER       PIC X(18) VALUE "July     August".

      03 FILLER       PIC X(18) VALUE "SeptemberOctober".

      03 FILLER       PIC X(18) VALUE "November December".

   02 FILLERh REDEFINES TableValues.

      03 Month OCCURS 12 TIMES PIC X(9).

 

01 MonthCount OCCURS 12 TIMES PIC 999 VALUE ZEROS.

 

01 MonthIdx           PIC 999.

 

01 HeadingLine          PIC X(19) VALUE " Month    StudCount".

 

01 DisplayLine.

   02 PrnMonth          PIC X(9).

   02 FILLER            PIC X(4) VALUE SPACES.

   02 PrnStudentCount   PIC ZZ9.

 

 

PROCEDURE DIVISION.

Begin.

   OPEN INPUT StudentFile

   READ StudentFile

      AT END SET EndOfStudentFile TO TRUE

   END-READ

   PERFORM UNTIL EndOfStudentFile

      ADD 1 TO MonthCount(MOBirth)

      READ StudentFile

         AT END SET EndOfStudentFile TO TRUE

      END-READ

   END-PERFORM

 

   DISPLAY HeadingLine

   PERFORM VARYING MonthIdx FROM 1 BY 1 UNTIL MonthIdx > 12

      MOVE Month(MonthIdx) TO PrnMonth

      MOVE MonthCount(MonthIdx) TO PrnStudentCount

      DISPLAY DisplayLine

   END-PERFORM.

 

   CLOSE StudentFile

   STOP RUN.

Given this Excel table below:

Cde County   ID     IdnCase          Applicant Last Name          Applicant First Name         Address1          Address2      City                   State        Zip

1                            9999979         Name                                    Joe                                       13 mockingbird lane              Somewhere1   NJ            99999

2                            9999980          Name2                                 Joe                                       14 mockingbird lane              Somewhere2   NJ            99999

3                            9999981         Someone                              Joe                                       15 mockingbird lane              Somewhere3   NJ            99999

4                            9999982         Somebody                            Joe                                       16 mockingbird lane              Somewhere4   NJ            99999

5                            9999983         Jones                                    Harry                                   17 mockingbird lane              Somewhere5   NJ            99999

6                            9999984         Jones2                                  Harry                                   18 mockingbird lane              Somewhere6   NJ            99999

7                            9999985         Jones3                                  Tom                                     19 mockingbird lane              Somewhere7   NJ             99999

8                            9999986         Jones4                                  Tom                                     20 mockingbird lane              Somewhere8   NJ             99999

9                            9999987         Jones5                                  Tom                                     21 mockingbird lane              Somewhere9   NJ             99999

10                          9999988         Jones6                                  moe                                     22 mockingbird lane              Somewhere10 NJ            99999

11                          9999989         Jones7                                  Tom                                     23 mockingbird lane              Somewhere11 NJ            99999

12                          9999990         Jones8                                  BOB                                     24 mockingbird lane              Somewhere12 NJ            99999

13                          9999991         Jones9                                  BOB                                     25 mockingbird lane              Somewhere13 NJ            99999

14                          9999992         Jones10                                BOB                                     26 mockingbird lane              Somewhere14 NJ            99999

15                          9999993         Jones11                                BOB                                     27 mockingbird lane              Somewhere15 NJ            99999

16                          9999994         Jones12                                BOB                                     28 mockingbird lane              Somewhere16 NJ            99999

17                          9999995         Jones13                                BOB                                     29 mockingbird lane              Somewhere17 NJ            99999

18                          9999996         Jones14                                BOB                                     30 mockingbird lane              Somewhere18 NJ            99999

19                          9999997         Jones15                                BOB                                     31 mockingbird lane              Somewhere19 NJ            99999

20                          9999998         Jones16                                BOB                                     32 mockingbird lane              Somewhere20 NJ            99999

21                          9999999         Jones17                                BOB                                     33 mockingbird lane              Somewhere21 NJ            99999

Here's how ChatGPT builds the properties for this table and turns it into COBOL input program:


IDENTIFICATION DIVISION.
PROGRAM-ID. ORGANIZE-TABLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TABLE-ENTRY.

   05 COUNTY-ID                   PIC X(10).
   05 CASE-ID                       PIC X(10).
   05 APPLICANT-LAST-NAME    PIC X(30).
   05 APPLICANT-FIRST-NAME   PIC X(30).
   05 ADDRESS1                  PIC X(30).
   05 ADDRESS2                  PIC X(30).
   05 CITY                              PIC X(30).
   05 STATE                           PIC X(2).
   05 ZIP                                PIC X(10).

 

01 INPUT-TABLE.
   05 INPUT-ENTRY OCCURS 21 TIMES.
      
      10 COUNTY-ID                   PIC X(10).
      10 CASE-ID                       PIC X(10).
      10 APPLICANT-LAST-NAME    PIC X(30).
      10 APPLICANT-FIRST-NAME   PIC X(30).
      10 ADDRESS1                   PIC X(30).
      10 ADDRESS2                   PIC X(30).
      10 CITY                               PIC X(30).
      10 STATE                            PIC X(2).
      10 ZIP                                 PIC X(10).
      

PROCEDURE DIVISION.
MAIN-PROCEDURE.
   OPEN INPUT 'YOUR_TABLE_FILE_NAME' 
   PERFORM READ-TABLE UNTIL EOF
   CLOSE INPUT 'YOUR_TABLE_FILE_NAME'
   DISPLAY 'Data has been read successfully.'
   STOP RUN.

READ-TABLE.
   READ TABLE-FILE INTO TABLE-ENTRY
      AT END SET EOF TO TRUE
   END-READ
   MOVE TABLE-ENTRY TO INPUT-ENTRY
```

Replace `'YOUR_TABLE_FILE_NAME'` with the name of the file containing your table data. This COBOL program reads the table data from the file, organizes it into an array of input entries, and stops when it reaches the end of the file.
 

```

bottom of page