Showing posts with label COBOL. Show all posts
Showing posts with label COBOL. Show all posts

Sunday, 26 August 2018

COBOL cheat sheet


  1. ENVIRONMENT DIVISION is optional.
  2. DATA DIVISION is optional.
  3. In IDENTIFICATION DIVISION, except PROGRAM-ID. everything else is optional.
  4. From Column 8-11, we call AREA-A where we should code DIVISIONS, SECTIONS, PARAGRAPHS and 01 group level items.
  5. From column 12-72, we call AREA-B where we will code rest of the Cobol statements other than DIVISIONS, SECTIONS, PARAGRAPHS and 01 group level items.
  6. If you specify SELECT OPTIONAL file ASSIGN TO filename, and if filename is not present physically then the program will NOT abend. Files with OPTIONAL can be opened with EXTEND, I/O or INPUT mode so that the file will be created else it return status code 35.
  7. FILE STATUS is used to identify the status of each operation performed against the file. FILE STATUS IS ws-status, where ws-status can be declared in working storage section with length of 2 bytes.  
  8. SELECT empfile ASSIGN TO empfileo where empfile is a logical file and empfileo is physical file. Inside the cobol program, for any operation on this file we have to use logical file name which is empfile and later all these operations will be reflected in physical file empfileo.
  9. If you are passing data from JCL to cobol program using PARM parameter, please declare two variables in linkage section viz., one is to store length of the data and the other one for actual data.
  10. Main program will have 'PROCEDURE DIVISION' where as sub-program or called program will have 'PROCEDURE DIVISION using [data-items]...
  11. USAGE clause reduce the storage space indirectly increasing the efficiency of the program. The default usage is DISPLAY and its not applicable for 66,77 and 88 level items. 
  12. COMP-1 => Left most 8 bits for exponent and remaining 24 bits for mantissa. COMP-2 => Left most 12 bits for exponent and 52 bits for mantissa. Both of the items store data in the format of Mantissa and exponent.
  13. Comp-3 => also called as packed decimal form where the data will be stored in memory as BCD (Binary Coded Decimal) format. Two digits can be stored in each byte and the number of bytes required for a variable is calculated using formula (n+1)/2. The low nybble of the least significant byte contains a sign. So even though we didn't define sign variable for COMP-3, a nibble is reserved for it. 
    Examples: 

    PIC S9(7) COMP-3.     Byte size = (7 + 1) / 2 = 4

    PIC S9(5)V99 COMP-3.  Byte size = (5 + 2 + 1) / 2 = 4

    PIC S9(6) COMP-3.     Byte size = (6 + 1) / 2 = 3.5, rounded to 4
Comp-3 fields reserve a nybble for the sign, even for "unsigned" values, so the following fields are still 4 bytes:
    PIC 9(7) COMP-3.     Byte size = (7 + 1) / 2 = 4
    PIC 9(6) COMP-3.     Byte size = (6 + 1) / 2 = 3.5, rounded to 4

You can also use the the formula (N/2) + 1, but just consider integer part as total number of bytes.
For instance, 
PIC S9(7) COMP-3   => Byte size = 7/2  + 1 => 3.5 + 1 => 4.5 => integer part means, 4.


14. START command is used in DYNAMIC mode to read the KSDS file. It will set the pointer to the next read for reading the record.
     START file-name KEY <relational-operator> <data-name>
             [INVALID KEY statements]
             [NOT INVALID KEY statements]
     END-EVALUATE.
     First key-value will be moved to data name and once START commands executes, the pointer will be placed at the starting of matched record.

15. READ NEXT is used to read the records sequentially after the record to which pointer was set by the START command based on key value.




       

Thursday, 27 August 2015

Difference between COPY and INCLUDE

  • COPY is used in simple COBOL program and INCLUDE is used in COBOL-DB2 Program.
  • COPY statements are executed at compile time. INCLUDE statements are executed at run time.

SORT in COBOL

External Sort is used to sort the file externally, like through JCL.

Internal sort is used to sort the file inside COBOL program using SORT verb.

Three files are used in the sort process in COBOL:

Input file – The file which needs to be sorted either in ascending or descending order.

Work file – The file which is used to hold the records during sort process. Input file records are transferred to this file for the sorting process. This file needs to be defined in File section under SD entry.

Output file – Final file which contains the records in the desired sorted order.

Syntax: 

SORT work-file ON ASCENDING-KEY/DESCENDING-KEY
      USING input-file GIVING output-file

Once the sorted records are moved to output file, the sort file will be deleted.

//INFILE DD DSN=input-file
//OUTFILE DD DSN=output-file
//SORTWK DD DSN=&&TEMP


Work file should be opened in I-O mode.

Wednesday, 19 August 2015

COBOL File status codes

00 Successful completion

02 Indexed files only. Possible causes:

For a READ statement, the key value for the current key is equal to the value of that same key in the next record in the current key of reference.

For a WRITE or REWRITE statement, the record just written created a duplicate key value for at least one alternate record key for which duplicates are allowed.

04  The length of the record being processed does not conform to the fixed file         attributes for that file.

05  The referenced optional file is not present at the time the OPEN statement is       executed.

06  Attempted to write to a file that has been opened for input.

07  Sequential files only. For an OPEN or CLOSE statement with the REEL/UNIT         phrase the referenced file is a non-reel/unit medium.

08  Attempted to read from a file opened for output.

09  No room in directory or directory does not exist.

10  No next logical record exists. You have reached the end of the file.

12  Attempted to open a file that is already open.

13  File not found.

14  Relative files only. The number of significant digits in the relative record             number is larger than the size of the relative key data item described for           that file. 

     Too many files open simultaneously (Micro Focus).

15 Too many indexed files open (Micro Focus).

16 Too many device files open (Micro Focus).

17 Record error: probably zero length (Micro Focus).

18  Read part record error: EOF before EOR or file open in wrong mode (Micro         Focus).

19  Rewrite error: open mode or access mode wrong (Micro Focus).

20  Device or resource busy (Micro Focus).

21 Sequentially accessed files only. Indicates a sequence error. The ascending        key requirements of successive record key values has been violated, or, the        prime record key value has been changed by a COBOL program between            successful execution of a READ statement and execution of the next                  REWRITE statement for that file.

22  Indexed and relative files only. Indicates a duplicate key condition. Attempt       has been made to store a record that would create a duplicate key in the           indexed or relative file OR a duplicate alternate record key that does not             allow duplicates.

23  Indicates no record found. An attempt has been made to access a record,           identified by a key, and that record does not exist in the file. Alternatively a       START or READ operation has been tried on an optional input file that is not       present.

24  Relative and indexed files only. Indicates a boundary violation. 
      
      Possible causes:

  • Attempting to write beyond the externally defined boundaries of a file.
  • Attempting a sequential WRITE operation has been tried on a relative file,   but the number of significant digits in the relative record number is larger   than the size of the relative key data item described for the file.


30  The I/O statement was unsuccessfully executed as the result of a boundary        violation for a sequential file or as the result of an I/O error, such as a data        check parity error, or a transmission error.

32   Too many Indexed files opened. This can also happen when a sequential            file is open for input and an attempt is made to open the same file for                output.(Run Time System (RTS) message by Micro Focus).

34   The I/O statement failed because of a boundary violation. This condition            indicates that an attempt has been made to write beyond the externally            defined boundaries of a sequential file.

35   An OPEN operation with the I-O, INPUT, or EXTEND phrases has been tried       on a non-OPTIONAL file that is not present.

37  An OPEN operation has been tried on a file which does not support the open      mode specified in the OPEN statement.

38  An OPEN operation has been tried on a file previously closed with a lock.

39  A conflict has been detected between the fixed file attributes and the                attributes specified for the file in the program. 
     This is usually caused by a conflict with record-length, key-length, key-               position or file organization.

      Other possible causes are:

  • Alternate indexes are incorrectly defined.
  • The Recording Mode is Variable or Fixed or not defined the same as when the file was created.


41  An OPEN operation has been tried on file already opened.

42  A CLOSE operation has been tried on file already closed.

43 Files in sequential access mode. The last I/O statement executed for the file,      before the execution  of a DELETE or REWRITE statement, was not a READ        statement.

44  A boundary violation exists. 

      Possible causes:

  • Attempting to WRITE or REWRITE a record that is larger than the largest,   or smaller than the smallest record allowed by the RECORD IS VARYING     clause of the associated file.
  • Attempting to REWRITE a record to a file and the record is not the same     size as the record being replaced.


46  A sequential READ operation has been tried on a file open in the INPUT or 
      I-O mode but no valid  next record has been established.

47  A READ or START operation has been tried on a file not opened INPUT or 
      I-O.

48  A WRITE operation has been tried on a file not opened in the OUTPUT, I-O,         or EXTEND mode, or on a file open I-O in the sequential access mode.

49  A DELETE or REWRITE operation has been tried on a file that is not opened         I-O.

91  For VSAM only.  Password Failure

92 For VSAM only. Logical Error

93 For VSAM only. Recourse not Available

94 For VSAM with CMPR2 compiler-option only: No file position indicator for             sequential request.

95 For VSAM only.  Invalid or incomplete file information

96 For VSAM under MVS: No DD statement specified for this file.
      FOR VSAM and SAM under VSE: No DLBL statement specified for this file.

97 For VSAM only.  OPEN statement execution successful: File integrity                   verified.

RENAMES Clause

RENAMES – used to regroup already defined set of data items.

RENAMES should be used only with 66 level variable. 

Example:

01 WS-DATE.                      
     05 WS-DD   PIC X(02).
     05 WS-MM   PIC X(02).
     05 WS-CC   PIC X(02).
       05 WS-YY   PIC X(02).
       66 WS-YYYY RENAMES WS-CC THRU WS-YY

REDEFINES Clause

REDEFINES is used to define the same memory area by different data items.

Rules:
  • Level numbers of data items REDEFINE should be same (See example below) .
  • REDEFINES cannot be used for level numbers 66 and 88.

Example:

01 WS-DATE1.                     
     05 WS-DD1  PIC X(02).
     05 WS-MM1  PIC X(02).
     05 WS-YYYY1  PIC X(04).
  01 WS-DATE2 REDEFINES WS-DATE1.
     05 WS-YYYY2  PIC X(04).
     05 WS-MM2  PIC X(02).
     05 WS-DD2  PIC X(02).

77 level, 88 level variables

77 level – 

Individual elementary data item.

77 level data items must be defined only in Working-storage and Linkage sections.

The data items defined in this level are neither subdivided to another data items nor they subdivided themselves. 

Example :

77  INDENT-C PIC X(16) VALUE SPACE.    
77  INDENT-I PIC X(40) VALUE 'ABCDEFG'.

88 level –  For condition name

Example:

05 VALID-DAY            PIC 9(2).                   
   88 VALID-DAY-OK                  VALUE 01 THRU 31.
   88 VALID-DAY-28                  VALUE 01 THRU 28.
   88 VALID-DAY-30                  VALUE 01 THRU 30.

STOP RUN, EXIT PROGRAM, GOBACK, GO TO

STOP RUN – This should be coded in the main program(Calling program) which specifies the end of the program and returns the control to OS.

EXIT PROGRAM – This should be coded in the Called program, which specifies the end of the called program and returns control back to calling program (Main program).

GOBACK – Can be coded both in Called program and Calling program.
When coded in calling program, it must be the last statement in the program and any statements after GOBACK will not be executed. It gives control back to OS. When coded in calling program, gives control back to called program.

GO TO – To pass the control to specific PARA. Any statement after GO TO will not be executed.

Static call and Dynamic call

Static call:

  • It specifies sub-program as literal (in quotes).
      CALL 'PGM-NAME' USING DATA-ITEM1 DATA-ITEM2
  • Based on the above declaration, linker will link object code of sub prog to main prog.
  • Main program and sub program loads are available in the load module of main program.
  • It occupies more memory while loading into main memory because the load is huge.
  • Processing is fast because both the loads are available at the same time and there is no need to load separately.
  • If the sub program is modified, the main program needs to be recompiled to link again.
  • Compiler option is NODYNAM.

Dynamic call:


  • It specifies sub-program as working-storage field where we pass the program name.
       CALL WS-PGM-NAME USING DATA-ITEM1 DATA-ITEM2
  • Main program and sub program loads are available separately.
  • It occupies less memory while loading into main memory, hence load size is small.
  • Processing is slow because each module is loaded separately. First main load module will be loaded and when the sub program is called, then the main load module will be replaced with sub program load and again when control comes back to main program then again main module will be loaded.
  • If the sub program is modified, the main program do not need to be recompiled.
  • Compiler option is DYNAM.

CALL BY REFERENCE:


  • Same memory area will be used for parameters both main and sub program.
  • Main program shares the address of parameters to sub program.
  • Any changes done to the value in sub program will reflect in main program.
  • Applicable for both static and dynamic calls.
       Syntax : CALL 'pgm-name' USING BY REFERENCE var-1 var-2...

CALL BY CONTENT:


  • Main program shares the contents of parameter to sub program.
  • Any changes to the content in sub program will not reflect in main program as it is internal to sub program.
  • Applicable for both static and dynamic calls.
       Syntax : CALL 'pgm-name' USING BY CONTENT var-1 var-2

CALL BY VALUE :


  • Main program shares the value of parameter(or literal) to sub program.
  • Any changes to the value in sub program will not reflect in main program because sub program will have access to only temporary copy of the sending item.
  • Applicable for both static and dynamic calls.
       Syntax : CALL 'pgm-name' USING BY CONTENT var-1 var-2

Difference between Subscript and Index


Subscript:
  • Represents number of occurrences of an array element.
  • It refers to the position of an array element where the element exists.
  • If it is single dimensional array, There is no need to declare this in data division because OCCURS clause automatically creates it.
  • Subscript value can be any positive integer and it always starts from 1.
  • Increment subscript refers to next occurrence of an array element.
  • In a one-dimensional table, the subscript corresponds to the row number and it is not required to declare in working storage section.
  • If it is multi dimensional array, a data item should be declared for subscript with S9(4) COMP.
Example:

01 WS-GROUP.
   05 WS-MONTH PIC X(3) OCCURS 12 TIMES.

To access second elements ws-month(2), for fourth ws-month(4) and so on...

Data item with OCCURS clause and without INDEXED BY can be accessed only by using subscript.

Index:
  • Number of displacement positions of an array.
  • Index should be declared by using INDEXED BY clause.
  • Index should be initialized to 1 by using SET operator before we use it.
  • Increment index by 1 increments the total length of single occurrence of an array.
  • Index should be incremented by SET <index-var> UP BY 1.
Example:
01 WS-GROUP.
   05 WS-MONTH PIC X(3) OCCURS 12 TIMES INDEXED BY WS-INDX.

To access second element, we need to first set the index to 2 like below

SET WS-INDX TO 2

Note : An index is similar to subscript, but internal values in the variables are different. Subscript refers to nonoccurence number of an element, for 2nd element it contains value 2
Index contains displacement from starting of an array, for 2nd element, it contains value 3(because array starts from 0, and 0,1,2 positions occupied by first occurrence and second occurrence starts from position 3). Similarly for 3rd occurrence, it contains value 6.

COBOL Editing data items

Z : Replaces all leading zeros with spaces
Ex. PIC  ZZ999
$ : One dollar character will be added at left most position
Ex. PIC $999
*(Asterisk) : Similar to Z, the leading zeros will be replaced with *
Ex. PIC **999
0(Zero) : Inserts zero at specified position.
Ex. PIC 999.00
B(BLANK) : Insert a blank at particular position.
Ex. PIC AABAA
BLANK WHEN ZERO : Display blank when the value of the variable is zero
Ex. PIC 99.99 BLANK WHEN ZERO
/(SLASH) : Insert slash at specific position
Ex. PIC 99/99/9999
,(Comma) : Insert comma at specific position
Ex. PIC 99,99,9999
.(Period) : Denotes decimal point in the numeric value.
Ex. PIC 99.99
-(Minus) : One sign ‘-‘ is allowed in the data item
Ex. PIC -999 OR 999-
+(Plus) : One sign ‘+‘ is allowed in the data item
Ex. PIC +999 OR 999+

Understanding COMP COMP-1 COMP-2 COMP-3

COMP
Data is stored as pure binary format.
The PIC clause of COMP data item should not contain other than 9 or S.
Depending on the size of data item, data is stored either in Half-word (2 bytes) or full-word (4 bytes).
9(1) – 9(4) occupies 2 bytes
9(5) – 9(9) occupies 4 bytes
S9(10) – S9(18) occupies 8 bytes
Example:
01 WS-VAR                    S9(2) COMP.
Occupies 2 bytes and data is stored in pure binary format internally.
COMP-1
This is used for single precision floating point items.
The data is represented in one word in floating point form.
It doesn’t have PIC clause and occupies 4 bytes.
Sign is present in the first bit of leftmost byte and the remaining 7 bits contain exponent. The last 3 bytes contain mantissa
Example: 
01 WS-VAR        USAGE IS COMP-1.
COMP-2
This is used for Double precision floating point items.
The data item is represented in two words in floating point form.
It doesn’t not have PIC clause and occupies 8 bytes.
Sign is present in the first bit in the left most byte and the remaining 7 bits contain exponent. The last 7 bytes contains mantissa.
Example: 

01 WS-VAR        USAGE IS COMP-2.
COMP-3
The data item is represented in Packed Decimal format
One digit takes half a byte.
The total length of the data item is
((n/2)+1), if n is EVEN
((n+1)/2), if n is ODD
The PIC clause allows S,9 and V.
We cannot move LOW-VALUES to these data items which results in Compilation error
COMP-3 values can be seen in PS by using HEX ON
Example: 
01 WS-VAR               PIC S9(05) COMP-3 VALUE +52636
WS-VAR will be displayed as
comp3
Total number of bytes consumed by WS-VAR is (5+1)/2 = 3 bytes