CASE WHEN statement in Easytrieve

Just like “EVALUATE WHEN” in Cobol, “Switch Case” in C, Easytrieve has got its equivalent “CASE WHEN” statement.

And, the when statement is not only capable of validating one particular value, it can also validate a range of values.

See the below sample Easytrieve program, which is self explanatory.

 CASE WS-NUM
  WHEN 1
     DISPLAY 'ONE'
  WHEN 2
     DISPLAY 'TWO'
  WHEN 3 THRU 99
     DISPLAY '3 TO 99'
 END-CASE

A complete program will look like,

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE FILE1
 WS-DAY  W 2 N
 JOB INPUT NULL
 WS-MONTH = 5
 CASE WS-MONTH
  WHEN 1
     DISPLAY 'SUNDAY'
  WHEN 2
     DISPLAY 'MONDAY'
  WHEN 3
     DISPLAY 'TUESDAY'
  WHEN 4
     DISPLAY 'WEDNESDAY'
  WHEN 5
     DISPLAY 'THURSDAY'
  WHEN 6
     DISPLAY 'FRIDAY'
  WHEN 7
     DISPLAY 'SATURDAY'
 END-CASE
 STOP

Reading VSAM file in Easytrieve

Declaring VSAM:

 FILE FILE2 VS

Reading VSAM file by Key

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE FILE1 FB(80 200)
   IN-REC 1 80 A
   IN-KEY 1 8 A
 FILE FILE2 VS
   F2-REC 1 80 A
   F2-KEY 1 8 A
   F2-VALUE 15 30 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
   OUT-KEY 1 8 A
   OUT-VALUE 10 30 A
 JOB INPUT NULL
 GET FILE1
 DO WHILE NOT EOF INFILE
   READ FILE2 KEY IN-KEY STATUS
   IF FILE2 : STATUS EQ 0
      MOVE IN-KEY TO OUT-KEY
      MOVE F2-VALUE TO OUT-VALUE
      PUT OUTFILE
   ELSE
      DISPLAY IN-KEY ' NOT PRESENT IN VSAM FILE'
   END-IF
   GET INFILE
 END-DO
 STOP

Arithmetic and Logical operators in Easytrieve

Arithmetic operations:

* Assignment
 WS-TEMP = 10
* Addition
 WS-TEMP = WS-TEMP + 2
* Subtraction
 WS-TEMP = WS-TEMP - WS-TEMP2
* Multiplication
 WS-TEMP = 10 * -5
* Division
 WS-TEMP = WS-TEMP / 5

Logical operators:

Equal IF WS-NUMBER EQ 12345
Not Equal IF WS-NUMBER NE 11111
Checking range A through B IF WS-NUMBER EQ 11111 THRU 22222
Checking a variable for Numeric IF WS-NUMBER2 NUMERIC
Checking for Zeros IF WS-NUMBER NOT ZEROS

REXX – STRREPLACE : Replace part of a string with new string

The Below Rexx function STRREPLACE can replace a part of a string with a new one

/*REXX*/
MYSTR = 'MY TEST STRING'
SAY MYSTR
MYSTR = STRREPLACE(MYSTR,"TEST","NEW")
SAY MYSTR
EXIT                                                               

/* A FUNCTION TO DO A STRING REPLACE */
STRREPLACE:
   ORIGINAL = ARG(1)
   OLDTXT   = ARG(2)
   NEWTXT   = ARG(3)
   NEWSTR = ORIGINAL
   DO WHILE POS(OLDTXT,NEWSTR) > 0
      NEWSTR = SUBSTR(NEWSTR, 1 , POS(OLDTXT,NEWSTR)-1) ||,
               NEW || SUBSTR(NEWSTR, POS(OLDTXT,NEWSTR) + LENGTH(OLDTXT))
   END
   RETURN NEWSTR

Introduction to REXX – Some examples

- A Rexx program should have a comment “/*rexx*/” in the first line
- Rexx statements are Case insensitive
- A rexx program can be executed by giving “EX” to the rexx member containing the rexx program

Hello World program

/************REXX***********/
 SAY "HELLO WORLD"
 EXIT

IF-ELSE in Rexx

/******REXX******/
/* EXECUTE SOME INSTRUCTIONS CONDITIONALLY */
 SAY 'ENTER NUMBER LESS THEN 10 '
 PULL A
 IF A<10 THEN
    DO
       SAY "CORRECT NUMBER!!"
    END
 ELSE SAY "WRONG!"
 EXIT

DO-Loops in Rexx

/*******REXX***********/
  /* SAY "HI" 5 TIMES */
  DO 5
     SAY "HI"
  END
  /**/
  DO C=1 TO 5
     SAY "HI"
  END
  /**/
  DO M=0.3 TO 1.5 BY .3
     SAY "HI"
  END
  /* INFINITE LOOP*/
 /*
 DO FOREVER
     SAY "HI"
 END
 */
 EXIT

DO WHILE and DO UNTIL

DO WHILE runs the loop all the times if the condition given validates to TRUE
DO UNTIL runs the loop all the times if the condition given validates to FALSE

/************REXX***********/
 DO WHILE A = 5
   SAY 'HI'
   /* SOMETHING THAT MODIFIES A */
 END

 DO UNTIL A = 5
   SAY 'HI'
   /* SOMETHING THAT MODIFIES A */
 END
 EXIT

SELECT statement in REXX

The below code shows how to use the SELECT clause. You can also note the use of NOT Operator “\”.

/******REXX******/
/* DECIDE WHAT RANGE A NUMBER IS IN */
 SAY 'ENTER A NUMBER'
 PULL A
 SELECT
    WHEN A>0 & A<100 THEN SAY "1-99"
    WHEN A\<100 & A<500 THEN SAY "100-499"
    WHEN \ (A<500 | A>=1000) THEN SAY "500-29"
    OTHERWISE SAY "OUT OF RANGE"
 END
 EXIT

Dividing one Number by another
PULL gets the value from the user.

/******REXX****************************************************/
 /* DIVIDING ONE NUMBER BY ANOTHER NUMBER */
 /**************************************************************/
 SAY 'DIVIDING ONE NUMBER BY ANOTHER NUMBER'
 SAY 'ENTER NUMERATOR:'
 PULL Z
 SAY 'ENTER DENOMINATOR:'
 PULL N
 IF N=0 THEN
   SAY 'DIVISION BY ZERO'
 ELSE DO
   SAY 'NUM / DENOM IS' Z/N
   SAY 'REST IS' Z//N
 END
 EXIT

A list of REXX functions can be found here.

You can also download a complete Tutorial PDF.

DO WHILE clause in Easytrieve

The syntax for a DO WHILE clause in Easytrieve is

 DO WHILE CONDITION1 [ AND CONDITION2 | OR CONDITION3 ]
   STATEMENTS TO EXECUTE
 END-DO

Please refer the below sample of code to use a do-while clause in Easytrieve.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT NULL
 WS-COUNT = 1
 DO WHILE WS-COUNT LE 15
   DISPLAY ' COUNT IS ' WS-COUNT
   WS-COUNT = WS-COUNT + 1
 END-DO
 STOP.

Using Files as Lookup tables in Easytrieve

Let us say, I have a lookup table file for Name of the Months. (DD name LOOKUP1 in our example program)

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
01 JANUARY
02 FEBRUARY
03 MARCH
04 APRIL
05 MAY
06 JUNE
07 JULY
08 AUGUST
09 SEPTEMBER
10 OCTOBER
11 NOVEMBER
12 DECEMBER

and I have another file (DD name ACTUAL) for which the month names have to be read from the above file.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
05
12
33
03

Output File should have the month numbers from the ACTUAL file and should have fetched the month name from LOOKUP1 file and put in theĀ outputĀ file.

Ie,

OUTFILE

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
05 MAY
12 DECEMBER
33 INVALID MON
03 MARCH

This Can be achieved by the following program which uses the LOOKUP1 file as a table.

The field names ARG and DESC should not be changed (because those are keywords).

And also the TABLE file should not have high level group variable for the full record.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE LOOKUP1 TABLE
      ARG              1    2    A
      DESC             4   15    A
 FILE ACTUAL
      ACTUAL-REC        1    80   A
        MONTH-LIST      1     2   A
 FILE OUTFILE
      OUT-REC          1   80  A
      OUT-MONTH-NUM    1    2  A
      OUT-MONTH-NAME   4   15  A
 JOB INPUT NULL
 GET ACTUAL
     DO WHILE NOT EOF ACTUAL
        MOVE MONTH-LIST TO OUT-MONTH-NUM
        SEARCH LOOKUP1 WITH MONTH-LIST , GIVING OUT-MONTH-NAME
        IF NOT LOOKUP1
           MOVE 'INVALID MON' TO OUT-MONTH-NAME
        END-IF
        PUT OUTFILE
        GET ACTUAL
     END-DO
 STOP.

Using MAXASSIGNEDVAL from SYSIBM.SYSSEQUENCES (IBM DB2 for Z OS)

SYSIBM.SYSSEQUENCES has a column called MAXASSIGNEDVAL, that gets assigned whenever the values for IDENTITY/SEQUENCE columns are getting generated (at least for the cache).

SELECT MAXASSIGNEDVAL
FROM SYSIBM.SYSSEQUENCES
WHERE SEQUENCEID =
(SELECT BSEQUENCEID FROM SYSIBM.SYSSEQUENCESDEP
WHERE DCREATOR = 'USER1'
AND DNAME = 'EMP_TABLE'
AND DCOLNAME = 'EMP_ID');

Creating (Declaring), Inserting data into DB2 temporary tables

Declaring (Creating)


DECLARE GLOBAL TEMPORARY TABLE
SESSION.EMP_TABLE
(EMPID INTEGER NOT NULL,
EMPNAME CHAR(20))
[ON COMMIT DELETE ROWS  | ON COMMIT PRESERVE ROWS]

Even if you do not give the qualifier SESSION, the table will be created as SESSION.EMP_TABLE.
Temporary tables cannot be created with our own qualifier.

Inserting

INSERT INTO SESSION.EMP_TABLE VALUES(1,'KARTHIK')

Fetching

SELECT * FROM SESSION.EMP_TABLE

Sort Card examples (IBM Mainframes)

Avoid duplicates in a particular field (say first 8 characters)

SORT FIELDS=(1,8,CH,A)
SUM FIELDS=NONE

Sort first 8 characters, and omit few values

SORT FIELDS=(1,8,CH,A)
OMIT COND=(1,8,CH,EQ,C'AAAAAAAA')

Sort first 8 characters, and include only few values

SORT FIELDS=(1,8,CH,A)
INCLUDE COND=(1,8,CH,EQ,C'AAAAAAAA',OR, 1,8,CH,EQ,C'BBBBBBBB')

Sort card to select only 15 characters from 36th position of my input file.

//SORTSTEP  EXEC PGM=SORT
//SYSOUT   DD SYSOUT=*
//SORTIN   DD DISP=SHR,
//            DSN=MYDSN.NAME
//SORTWK01 DD UNIT=SYSDA,SPACE=(CYL,2000)
//SORTWK02 DD UNIT=SYSDA,SPACE=(CYL,2000)
//SORTWK03 DD UNIT=SYSDA,SPACE=(CYL,2000)
//SYSIN    DD *
 SORT FIELDS=COPY
 OUTFIL CONVERT,
 OUTREC=(1:36,15,65X)
/*
//SORTOUT  DD DISP=(NEW,CATLG,DELETE),
//            SPACE=(CYL,(250,100),RLSE),
//            UNIT=DISK,
//            DCB=(RECFM=FB,LRECL=80,BLKSIZE=0),
//            DSN=MYDSN.OUTPUT.NAME

The 65X is given to fill the remaining 65 bytes of the output file with spaces.

Next Page »