Sort card – replace selective fields (IFTHEN,WHEN)

Lets say, we want to replace the word ‘TEST’ in position 16 into ‘TEMP’

----+----1----+----2----+----3----+----4----+----5----+----6----+----7-
123423452345   TEST  ARASDRGSDFGS
12342356563465 AAAA  JHFDGHDFSGDSF
7853425632456  TEST  JDFSGGDSFHJGGH
SORT FIELDS=COPY
INREC IFTHEN=(WHEN=(16,4,CH,EQ,C'TEST'),OVERLAY=(16:C'TEMP'))

Lets say, we want to replace the word ‘TEST’ in position 16 into ‘TEMP’, and ‘TEMP’ in position 36 to ‘TEST’

----+----1----+----2----+----3----+----4----+----5----+----6----+----7-
123423452345   TEST  ARASDRGSDFGS
12342356563465 AAAA  JHFDGHDFSGDSF TEMP  
7853425632456  TEST  JDFSGGDSFHJGGH
SORT FIELDS=COPY
INREC IFTHEN=(WHEN=(16,4,CH,EQ,C'TEST'),OVERLAY=(16:C'TEMP')),
      IFTHEN=(WHEN=(36,4,CH,EQ,C'TEMP'),OVERLAY=(16:C'TEST'))

also, read about FINDREP, more about IFTHEN

ICETOOL to get maximum, minimum value records

ICETOOL SELECT FIRST / LAST to get largest / smallest values within a set

Suppose we have a file with Branch name, Account number, Transaction amounts. like,

Branch  Account-num  Transaction-Amount
----+----1----+----2----+----3----+----4----+
Chennai   10011     00523
Chennai   10011     00010
Bangalore 10011     00056
Bangalore 10011     00670
Chennai   10012     00200
Chennai   10012     00235
Chennai   10012     00750
Bangalore 10012     00750
Bangalore 10012     00034

and, we want to get the maximum value transactions for every account. like,

Branch  Account-num  Transaction-Amount
----+----1----+----2----+----3----+----4----+
Bangalore 10011     00670
Bangalore 10012     00750
Chennai   10011     00523
Chennai   10012     00750

this can be done using ICETOOL as,

//TOOLIN DD *
 SELECT FROM(INFILE) TO(OUTFILE) ON(BRANCH) ON(ACCOUNT) -
 FIRST USING(CTL1)
/*
//CTL1CNTL DD *
 SORT FIELDS=(BRANCH,A,ACCOUNT,A,TRANS-AMT,D)
/*
//SYMNAMES DD *
 BRANCH,1,10,CH
 ACCOUNT,11,5,ZD
 TRANS-AMT,21,5,ZD
/*

In the above ICETOOL step, we are sorting the file in the order of Branch name (Ascending), Account number (Ascending), Transaction amount (Descending).

(If you want to select the smallest/minimum value transactions only, then sort the Transaction amount in Ascending order).

Then for each Branch and Account, we are selecting the first record only.

what are SYMNAMEs?

ICETOOL to copy files

Direct copy of multiple files. Copy files from DD names INFILE1, INFILE2, INFILE3 to OUTFILE1, OUTFILE2, OUTFILE3

//TOOLIN DD *
 COPY FROM(INFILE1) TO(OUTFILE1)
 COPY FROM(INFILE2) TO(OUTFILE2)
 COPY FROM(INFILE3) TO(OUTFILE3)

Copy of multiple files with some processing. Copy files from DD names INFILE1, INFILE2, INFILE3 to OUTFILE1, OUTFILE2, OUTFILE3

//TOOLIN DD *
 COPY FROM(INFILE1) TO(OUTFILE1) USING(CTL1)
 COPY FROM(INFILE2) TO(OUTFILE2) USING(CTL2)
 COPY FROM(INFILE3) TO(OUTFILE3) USING(CTL3)
/*
//CTL1CNTL DD *
 SORT FIELDS=(1,5,CH,A)
/*
//CTL2CNTL DD *
 SORT FIELDS=(6,5,CH,A)
/*
//CTL3CNTL DD *
 SORT FIELDS=COPY
 OUTFIL CONVERT,
 OUTREC=(1:5,80,20X)

Easytrieve program to parse a string

Below program parses a string that contains 3 words which are separated by space.

FULL-NAME contains ‘KARTHIK KAR THIK’, which is split into First name, Middle name and Last name.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 DEFINE IND1 W 3 N VALUE 1 
 DEFINE IND2 W 3 N VALUE 1 
 DEFINE FLAG W 1 N VALUE 1 
 DEFINE FULL-NAME W 30 A VALUE 'KARTHIK KAR THIK' 
 DEFINE FIRST-NAME W 15 A 
 DEFINE MID-NAME W 15 A 
 DEFINE LAST-NAME W 15 A 
 DEFINE FULL-NAME-ARRAY  FULL-NAME  1 A OCCURS 30 
 DEFINE FIRST-NAME-ARRAY FIRST-NAME 1 A.OCCURS 15 
 DEFINE MID-NAME-ARRAY   MID-NAME   1 A OCCURS 15 
 DEFINE LAST-NAME-ARRAY  LAST-NAME  1 A OCCURS 15 
 JOB INPUT NULL 
   DO WHILE IND1 LE 30 
      IF FULL-NAME-ARRAY(IND1) NE ' '
         CASE FLAG 
            WHEN 1 
              MOVE FULL-NAME-ARRAY(IND1) TO FIRST-NAME-ARRAY(IND2)
            WHEN 2 
              MOVE FULL-NAME-APRAY(IND1) TO MID-NAME-ARRAY(IND2) 
            WHEN 3 
              MOVE FULL-NAME-ARRAY(IND1) TO LAST-NAME-ARRAY(IND2) 
         END-CASE 
         IND2 = IND2 + 1 
      ELSE 
         IND2 = 1 
         FLAG = FLAG + 1
      END-IF 
      IND1 = IND1 + 1 
   END-DO 
   DISPLAY FIRST-NAME 
   DISPLAY MID-NAME 
   DISPLAY LAST-NAME 
STOP

Output will be,

KARTHIK
KAR
THIK

COBOL – PERFORM a SECTION

Below sample COBOL program explains how to PERFORM a SECTION.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.       
       PROGRAM-ID.    KARTEST5.       
      * perform a SECTION             
       ENVIRONMENT DIVISION.          
       DATA DIVISION.                 
       PROCEDURE DIVISION.            
           PERFORM MY-SECTION.        
           GOBACK.
       MY-SECTION SECTION.            
           DISPLAY 'INSIDE MY-SECTION'
           EXIT.

Output of this program will be

INSIDE MY-SECTION

COBOL – IF ELSE ENDIF statement

Below sample COBOL program explains the IF ELSE END-IF statement. You can avoid using END-IF by using dot (‘.’) as scope terminator. But it is not a good practice. better, always code a END-IF statement as scope terminator.

‘THEN’ is optional. If you want, THEN you can use it.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.           
       PROGRAM-ID.    KARTEST4.           
      * IF statement in cobol             
       ENVIRONMENT DIVISION.              
       DATA DIVISION.                     
       WORKING-STORAGE SECTION.           
        01 MY-NUMBER  PIC 9(2) VALUE 2.   
       PROCEDURE DIVISION.                
      * simple IF statement
           IF MY-NUMBER = 2               
              DISPLAY 'TWO'               
           END-IF                         
      * IF ELSE statement
           IF MY-NUMBER IS EQUAL TO 2 THEN
              DISPLAY 'TWO'               
           ELSE
              DISPLAY 'NOT TWO'
           END-IF              
      * nested IF ELSE statement
           IF MY-NUMBER = 1
              DISPLAY 'ONE'               
           ELSE
              IF MY-NUMBER = 2
                 DISPLAY 'TWO'
              ELSE
                 DISPLAY 'NEITHER ONE NOR TWO'
              END-IF
           END-IF              
           GOBACK.

Output of this program will be

TWO
TWO
TWO

Complete syntax of IF Command (COBOL)

COBOL–Working storage variables

Working Storage section comes under DATA division. Different datatypes of COBOL are explained here.

Below sample cobol program declares and Displays a working storage variable named ‘MY-STRING’.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION.                        
       PROGRAM-ID.    KARTEST3.                        
      * define and use a working storage variable      
       ENVIRONMENT DIVISION.                           
       DATA DIVISION.                                  
       WORKING-STORAGE SECTION.                        
        01 MY-STRING  PIC X(20) VALUE 'HELLO WORLD ! '.
       PROCEDURE DIVISION.                             
           DISPLAY MY-STRING.                          
           GOBACK.

Output of this program will be

HELLO WORLD !

COBOL – Hello World program

Below sample cobol program has the minimal set of statements required. It contains all the four divisions and PROGRAM-ID is a mandatory statement which will have the program name.

IDENTIFICATION DIVISION
ENVIRONMENT DIVISION        
DATA DIVISION              
PROCEDURE DIVISION

Divisions – > Section –> Paragraph –> Statement.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
       IDENTIFICATION DIVISION. 
       PROGRAM-ID.    KARTEST2. 
       ENVIRONMENT DIVISION. 
       DATA DIVISION. 
       PROCEDURE DIVISION. 
           DISPLAY 'HELLO WORLD ! ' .
           GOBACK.

Output of this program will be

HELLO WORLD !

simple and nested IF-ELSE-ENDIF statement in Easytrieve

IF ELSE ENDIF statement in Easytrieve is similar to how we do in COBOL.

Below are some sample programs that will help you understand.

 LIST ON
 DEFINE WS-FIVE W 5 N VALUE 5
 JOB INPUT NULL
 IF WS-FIVE EQ 5
    DISPLAY 'WS-FIVE IS 5'
 END-IF
 STOP
 LIST ON
 DEFINE WS-FIVE W 5 N VALUE 5
 JOB INPUT NULL
 IF WS-FIVE EQ 5
    DISPLAY 'WS-FIVE IS 5'
 ELSE 
    DISPLAY 'WS-FIVE IS NOT 5'
 END-IF
 STOP
 LIST ON
 DEFINE WS-NUM W 5 N VALUE 6
 JOB INPUT NULL
 IF WS-NUM EQ 5
    DISPLAY 'WS-NUM IS 5'
 ELSE 
    IF WS-NUM EQ 6
       DISPLAY 'WS-NUM IS 6'
    ELSE 
       DISPLAY 'WS-NUM IS NOT 5 OR 6'
    END-IF
 END-IF
 STOP