Wednesday, November 28, 2012

COBOL Reviewer Part 2

What is the COBOL SORT verb?

•    The SORT verb takes one or more files of input records and sorts them in ascending or descending order by one or more programmer specified keys.


•    The result is a single sorted file.


•    Format: 
SORT  sd-file-name
            ON ASCENDING/DESCENDING KEY  sd-key-name

            |USING input-file-name                         |
            |INPUT PROCEDURE IS routine-name|

            |GIVING sorted-file-name                         |
            |OUTPUT PROCEDURE IS routine-name|.


   sd-file-name      a temporary sort work file
                             SD in the DATA DIVISION
                             SELECT statement in the ENVIRONMENT DIVISION
                             MUST be CLOSED when the sort is called


   sd-key-name     key field from the SD record description


   USING input-file-name        this is the file that will be sorted
                                    FD in the DATA DIVISION
                                    SELECT stmt in the ENVIRONMENT DIVISION
                                    MUST be CLOSED when the sort is called
  
  
   INPUT PROCEDURE IS routine-name
  
      - routine-name is a programmer coded routine that will
        select which records are to be used in the sort
     
      - input procedure pseudocode:
     
              OPEN input-file-name
        
              READ the first record
         
              While there are input records
                    Modify the current record
                    MOVE record to sd-file-record
                    RELEASE sd-file-record
                    READ next record
              Endwhile
         
              CLOSE input-file-name

         
       - To release a record to the sort:
      
              RELEASE sd-file-record [FROM ws-area].
         
 this is equivalent to a WRITE in COBOL except that its performed on an SD file and is used ONLY in an input procedure
           
   GIVING sorted-file-name     this is the resulting sorted file
                                    FD in the DATA DIVISION
                                    SELECT stmt in the ENVIRONMENT DIVISION
                                    MUST be CLOSED when the sort is called
  
  
   OUTPUT PROCEDURE IS routine-name
  
      - routine-name is a programmer coded routine that will
        manipulate the records AFTER they are sorted

      - output procedure pseudocode:
     
              OPEN sorted-file-name
        
              RETURN the first sd-file-record
         
              While there are input records
                    Modify the sorted record
                    MOVE sd-file-record to sorted-file-record
                    WRITE sorted-file-record
                    RETURN the next sd-file-record
              Endwhile
         
              CLOSE sorted-file-name

         
       - To return a record:
      
              RETURN sd-file-name [INTO ws-area]
                    [AT END do something]
                    [NOT AT END do something else]
              END-RETURN.
         
         this is equivalent to a READ in COBOL except that it is performed on an SD file and is used ONLY in an output procedure


•    The COBOL SORT verb calls SYNCSORT to do its work.
•    There are some "special" registers that are set by the SORT verb and can be set or tested by a programmer.


SORT-RETURN        after a SORT this will contain a return code that can be tested
              
                       0        success
                       16       failure
              
 if a programmer moves a non-zero value to this register, the sort will stop on the next RETURN or RELEASE
              
SORT-FILE-SIZE  equivalent to SYNCSORTs FILSZ

SORT-MESSAGE  can use this one to specify an 8 byte ddname for SYNCSORT to write its messages to
              
                       In COBOL:  MOVE 'SORTOUT' TO SORT-MESSAGE.
              
                       In JCL:  //SYSOUT   DD  SYSOUT=*
                                    //SORTOUT  DD  SYSOUT=*



How do you SORT in a COBOL program? Give sort file definition, sort statement syntax and meaning.

•    The COBOL SORT statement is used in the PROCEDURE DIVISION to place records from an input file in a temporary work file where their order will be rearranged. This work file is referred to as the sort work file. Once rearranged, the records can be placed back in the original input file, placed in a separate output file, or processed by the program directly from the sort work file. 


•    ENVIRONMENT DIVISION entries
The input file, the sort file, and, if used, the output file must each be defined by a SELECT statement in the INPUT-OUTPUT SECTION, FILE-CONTROL paragraph. As with any other file, the SELECT statement for the sort file specifies both the internal and external filenames for the program and for the Operating System.

    SELECT SORT-FILE ASSIGN TO DISK “SORTWORK.TMP”.

In this example, the internal filename is SORT-FILE and the external filename is SORTWORK.TMP. These are programmer-supplied words, as with any file definition, and are chosen because they are descriptive. The DOS file extension TMP is chosen to remind you that this is a temporary file.


•    DATA DIVISION entries
In the FILE SECTION, the input and output files would be described as usual, using FD and record description entries. For the SORT-FILE, in place of an FD entry, a sort work file has a similar SD entry, which stands for Sort Description. The record definition following the SD entry is called the sort record and must be broken down enough to show the position and size of the sort key field(s). Multiple keys can be defined. All other fields can be defined as filler entries if not used in the Procedure Division. The total number of characters (sum of the PIC sizes) within this record must match the number of characters in the records to be sorted.

    SD    SORT-FILE.
    01    SORT-RECORD.
        02                          PIC X(20).
        02    SORT-KEY   PIC X(5).
        02                          PIC X(30).
        02    SR-UNITS    PIC 999.
        02    SR-COST     PIC 9(5)V99.
        02                         PIC X(15).


In this example, SD is used instead of FD to describe the sort file in this division. The file name must be the same as the internal file name in the Environment Division. The record is described to define the name, position, data type, and size of the key field for use with the SORT verb in the Procedure Division. Other fields that may be referenced by the program’s procedures are also defined.


•    PROCEDURE DIVISION statement
The sort work file and the sort key field(s) are among the entries named in a SORT statement. The name of the work file must follow the verb SORT. This name must be the same as the name used in the SELECT and SD entries. Records can be arranged in either ASCENDING or DESCENDING sequence by key field(s) specified. When multiple key fields are used, they must be listed from major to minor. USING and GIVING clauses are used to specify the name of the input data file and the name of the output data file respectively.

    SORT SORT-FILE
        ASCENDING SORT-KEY,
        USING INPUT-FILE,
        GIVING OUTPUT-FILE.


In this example, the SORT verb would perform these procedures:
1.    The file named INPUT-FILE would automatically be opened for input, records read, rearranged, and stored in the sort’s work file in ascending sequence according to the values in the key field of each record, and the file closed.
2.    The file named OUTPUT-FILE would automatically be opened for output, the sorted records written to the file, and the file closed.

To sort the records, the programmer does not code any OPEN, CLOSE, READ, or WRITE statements. The SORT causes all these operations to occur. After the SORT verb is complete, the programmer may open the resulting sorted file for input and process the records normally (by using the verbs OPEN, READ, CLOSE). This may be accomplished in the same program or in a separate one.


How do you define a sort file in JCL that runs the COBOL program?

•    Use the SORTWK01, SORTWK02,..... dd names in the step. Number of sort datasets depends on the volume of data being sorted, but a minimum of 3 is required.



What is the SORT collating sequence in mainframe COBOL?

•    Sorting takes place on the relative binary values of the characters in the key field(s) of the sort record.
•    Mainframe code sets is EBCDIC (Extended Binary Coded Decimal Interchange Code). Collating sequence is Space, a-z, A-Z, 0-9.

No comments:

Post a Comment