November 2004 Technical Tip Lookup Tables with Easytrieve

Anyone who has ever coded table load and lookup logic with another language will appreciate the ease with which one can do the same thing in Easytrieve.

We'll keep this simple...assume the input file (ddname MYDATA) has two fields - name and state code - as shown here:

//MYDATA   DD *
BILL    UT
NORM    CA
DAVE    AZ
TERRI   WI
BESSIE  IL
JIM     NV

Our lookup table (ddname MYSTATES) has two fields - state code and state name - as shown here:

//MYSTATES DD *
CA CALIFORNIA
IL ILLINOIS
UT UTAH
WI WISCONSIN

The lookup table is defined in the Easytrieve program as follows:

FILE MYSTATES TABLE
   ARG         1   2  A
   DESC        4  15  A

  • Note the use of the keyword TABLE
  • Each TABLE file has two fields - ARG and DESC. Those names are required!
  • The table must be sequenced by the ARG.

We define a working field to hold the (found?) state name:

* WORK FIELD
   STATE-NAME  W  15  A

The table lookup logic is included in the JOB portion of the Easytrieve program as follows:

JOB INPUT MYDATA
   SEARCH MYSTATES WITH STATE-CD GIVING STATE-NAME

   IF MYSTATES
      * THEN FOUND
   ELSE
      STATE-NAME = 'UNKNOWN'
   END-IF

   PRINT MYREPORT

Note the use of the IF statement which checks for a found/not found condition. This is required because if the argument is not found in the table, the target field (STATE-NAME here) is left unchanged. This could also be coded as:

   IF NOT MYSTATES
      STATE-NAME = 'UNKNOWN'
   END-IF

The complete Easytrieve program with JCL is as follows:

//* JOBCARD HERE
//STEP010 EXEC PGM=EZTPA00    
//SYSPRINT DD SYSOUT=*        
//MYSTATES DD *               
CA CALIFORNIA                 
IL ILLINOIS                   
UT UTAH                       
WI WISCONSIN                  
//MYDATA   DD *               
BILL    UT                    
NORM    CA                    
DAVE    AZ                    
TERRI   WI                    
BESSIE  IL                    
JIM     NV                    
//SYSIN    DD *               
FILE MYSTATES TABLE           
   ARG         1   2  A       
   DESC        4  15  A                            
                                                   
FILE MYDATA                                        
   NAME        1   8  A                            
   STATE-CD    9   2  A                            
                                                   
* WORK FIELD                                       
   STATE-NAME  W  15  A                            
                                                   
JOB INPUT MYDATA                                   
   SEARCH MYSTATES WITH STATE-CD GIVING STATE-NAME 
                                                   
   IF MYSTATES                                     
      * THEN FOUND                                 
   ELSE                                            
      STATE-NAME = 'UNKNOWN'                       
   END-IF                                          
                                                   
   PRINT MYREPORT                                  
                                                   
REPORT MYREPORT NOADJUST         
   LINE NAME STATE-CD STATE-NAME 

The complete output is as follows:

  NAME     STATE-CD     STATE-NAME    

BILL          UT      UTAH            
NORM          CA      CALIFORNIA      
DAVE          AZ      UNKNOWN         
TERRI         WI      WISCONSIN       
BESSIE        IL      ILLINOIS        
JIM           NV      UNKNOWN 

Want to know more about Easytrieve? Give us a call! You can count on Caliber Data Training for top quality mainframe and/or internet programming education.


Go to the articles index. Written by Bill Qualls. Copyright 2004 by Caliber Data Training 800.938.1222