EDIT MASKS – Masking variables in Easytrieve

have you already read Easytrieve Variable declaration?

For creating reports, or printing (displaying) numeric variables, we will always need masking. It can be used to print the decimal character for the variables that have implied decimal

 WS-SSN W 9 N MASK '999-99-9999'
 ...
 MOVE 123456789 TO WS-SSN
 DISPLAY ' WS-SSN : ' WS-SSN

This will be printed as

 WS-SSN : 123-45-6789

Edit mask pattern is created by using combinations of the following characters:

9 — causes a digit to print. (including leading zeros)

Z — causes a digit to print (except for leading zeros)

$ — causes a currency symbol to print prior to the first non-zero digit

more examples to come..

DISPLAY statement in Easytrieve

DISPLAY statement in Easytrieve is exactly the same as in COBOL.

 WS-VAR1 W 20 A VALUE 'I AM INITIALIZED HERE'
 ...
 DISPLAY 'value of WS-VAR1 is: ' WS-VAR1

This will be printed as

 value of WS-VAR1 is: I AM INITIALIZED HERE

If you want to continue the DISPLAY statement in multiple lines, use ‘+’ symbol as continuation character

 WS-VAR1 W 20 A VALUE 'I AM INITIALIZED HERE'
 ...
 DISPLAY 'value of WS-VAR1 is: ' +
         WS-VAR1

Update / RE WRITE VSAM file in Easytrieve

Updating/ REWRITING a VSAM record:
VSAM declaration should look like,

 FILE FILE2 VS UPDATE

FILE2 is the DD name – must have DISP=OLD

Reading VSAM file by Key and updating the record

----+----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 UPDATE
   F2-REC 1 18 A
   F2-KEY 1 8 A
   F2-VALUE 9 10 N
 JOB INPUT NULL
 GET FILE1
 DO WHILE NOT EOF FILE1
   READ FILE2 KEY IN-KEY STATUS
   IF FILE2 : FILE-STATUS EQ 0
      F2-VALUE = F2-VALUE + 5
      WRITE FILE2 UPDATE
   ELSE
      DISPLAY IN-KEY ' NOT PRESENT IN VSAM FILE'
   END-IF
   GET INFILE
 END-DO
 STOP

JOB Input statement in Easytrieve

Job statement is the main section or Procedure division of the easytrieve.
Let us see the different types of JOB statements.
JOB INPUT NULL
If you want to process the files yourself and you dont want the Easytrieve to read/match files for you, please use JOB INPUT NULL.
e.g,

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT NULL
 GET INFILE
 DO WHILE NOT EOF INFILE
   PUT OUTFILE FROM INFILE
   WS-COUNT = WS-COUNT + 1
   GET INFILE
 END-DO
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP

JOB INPUT
In the above case, if you want the input file to be automatically read, record by record, use the below format
JOB INPUT

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT INFILE
   PUT OUTFILE FROM INFILE
 GO TO JOB
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP

In the above program, the JOB statement automatically reads a record from INFILE, each time it executes. But what the GO TO JOB is doing?
It is looping the program to read all the records from the INFILE until End of the file. Otherwise, the STOP statement will terminate the program after the first record itself. There is another smarter way to achieve this.

JOB INPUT … END

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT INFILE
   PUT OUTFILE FROM INFILE
 END
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP

The END statement makes the code to look better. It still reads all the records from INFILE and writes to OUTFILE.

File matching : Synchronized files

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE FILE1
          F1-REC           1    50   A
          F1-KEY           1     2   A
 FILE FILE2
          F2-REC           1    50   A
          F2-KEY           7     2   A
 FILE OUTFILE
 OUT-REC     1  50 A
 JOB INPUT (FILE1 KEY(F1-KEY)  FILE2 KEY(F2-KEY))
 IF MATCHED
     PUT OUTFILE FROM FILE2
 END-IF
 GO TO JOB

Easytrieve MACROs (Copybook)

Easytrieve MACROs are similar to COBOL Copybooks. i.e, they are used to reuse the layout of a file.

A Sample Macro:

 MYDATA.SET.MACROS(MYFILE)
 MACRO
 INFILE
 IN-REC  1 80 A
 IN-KEY  1   8 A

The first line of a Macro should be MACRO.

How to use this in an Easytrieve program?

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
 %MYFILE
*
 JOB INPUT NULL
 GET INFILE
...
 STOP.

The statement %INFILE includes the Macro MYFILE, into the program.
The dataset containing the Macros should be mentioned in the JCL as

//PANDD DD DSN=MYDATA.SET.MACROS,DISP=SHR

Macros with Parameters:
Macros can take some parameters.
See the example below.

 MYDATA.SET.MACRO(MYFILE)
 MACRO FNAME PREFIX
&FNAME.
&PREFIX.-REC  1 80 A
&PREFIX.-KEY  1   8 A

FNAME and PREFIX are the parameters. Use an &VARIABLE. to substitute a variable (An Ampersand in the beginning and a dot in the end).

How to use this in an Easytrieve program?

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
 %MYFILE IN IN
*
 JOB INPUT NULL
 GET INFILE
...
 STOP.

%MYFILE IN IN substitutes as FNAME=IN, PREFIX=IN

If you miss the PANDD in the JCL, or MACRO statement in the MACRO member, you will see an error as

*******B006 MACRO SYSTEM - PDS , ERROR IN MACRO FILE

Equivalent functionality for REDEFINES in Easytrieve

have you already read Easytrieve Variable declaration?

In COBOL, REDEFINES verb can be used to declare variables that share a same memory location.
In Easytrieve also, there is a similar functionality.
For example, the ALPHA and NUMERIC variables given below share the same 5 bytes.

 WS-ALPHA   W        5 A
 WS-NUMERIC WS-ALPHA 5 N

Since the starting position for WS-NUMERIC is given as WS-ALPHA, both of them start from the same memory location.

In case, if you want the same functionality in a File variable,

 FILE INFILE
 IN-REC   1 80 A
 IN-ALPHA     1  5 A
 IN-NUMERIC   1  5 N
 FILLER       6 75 A

OCCURS Clause (arrays) in Easytrieve

This post is a part of Easytrieve variable declaration. You may consider reading it first.

As in COBOL, OCCURS clause can be used to declare an Array in Easytrieve also.  As in most of the languages, Arrays can be accessed using INDEX or SUBSCRIPS. Using Index is an efficient option. Subscripts are easier to code.
Below is a sample program.

Declare and use an array by Index

when using Index for accessing an array element, you first set the index. In the below example it is set to 15. and in the next step, you read (or write to) that particular indexed element.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
 WS-ALPHA W 26 A VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 WS-ALP-ARR WS-ALPHA 1 A OCCURS 26 INDEX ALPHA-NDX
*
 JOB INPUT NULL
* PRINT 16TH ALPHABET
 ALPHA-NDX = 15
 DISPLAY '16TH ALPHABET IS ' WS-ALP-ARR
 STOP.

Accessing an array by Subscripts 

when using subscript, you can directly read the n-th element of an array – by passing the subscript in brackets.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
 WS-DAYS-OF-WEEK W 10 A OCCURS 7
*
 JOB INPUT NULL
 MOVE 'SUNDAY' TO WS-DAYS-OF-WEEK(1)
 MOVE 'MONDAY' TO WS-DAYS-OF-WEEK(2)
 MOVE 'TUESDAY' TO WS-DAYS-OF-WEEK(3)
 MOVE 'WEDNESDAY' TO WS-DAYS-OF-WEEK(4)
 MOVE 'THURSDAY' TO WS-DAYS-OF-WEEK(5)
 MOVE 'FRIDAY' TO WS-DAYS-OF-WEEK(6)
 MOVE 'SATURDAY' TO WS-DAYS-OF-WEEK(7)
 DISPLAY '4 TH DAY IS ' WS-DAYS-OF-WEEK(4)
 STOP.

Want another example?

One of the use of array is to parse a string character by character – Parse a String into words.

FILE-STATUS and EOF (End of File) in Easytrieve

In Easytrieve, whenever you perform an operation to a File, the STATUS of the operation can be checked using the FILE-STATUS variable.

For Normal Files,
FILE-STATUS = 0 means, Successful GET or PUT or READ or WRITE operation.

 GET INFILE
 IF INFILE:FILE-STATUS NE 0
     DISPLAY 'ERROR IN READING FILE'
 END-IF

End of a File can be identified using EOF keyword.
i.e,

 IF EOF INFILE
     DISPLAY 'END OF FILE'
 END-IF

In case of a VSAM file, FILE-STATUS can mean any of the following

FILE-STATUS Meaning
0 Successful operation
4 End of File
8 Duplicate key during a Write/PUT (or)

Additional records with the same key exist in the

alternate index during a GET operation

12 Keys not in Sequence during PUT/WRITE
16 Record not found during READ

GET and PUT statements in Easytrieve

GET and PUT statements are used to Read and Write to/from a file.
See a sample Easytrieve program below.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT NULL
 GET INFILE
 DO WHILE NOT EOF INFILE
   MOVE IN-REC TO OUT-REC
   PUT OUTFILE
   WS-COUNT = WS-COUNT + 1
   GET INFILE
 END-DO
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP 

Also learn about READ and WRITE statements.

There is another simple way to do this, using PUT FROM

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
...
 JOB INPUT NULL
 GET INFILE
 DO WHILE NOT EOF INFILE
   PUT OUTFILE FROM INFILE
   WS-COUNT = WS-COUNT + 1
   GET INFILE
 END-DO
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP