How to write CA Earl Report Requests - Conditional Logic, Branching and Labels
See KDs - TEC291378, TEC323377, TEC265260, TEC265262. TEC274567, TEC291378
Please see the KDs listed for additional supportive information that will assist in this process. In this article, we'll discuss how to change the processing flow of an CA Earl program using IF and GOTO statements. We'll also look at the predefined labels that are available in every CA Earl program.
Consider the following CA Earl request from the previous article:
NOTE =======================================================
NOTE This is the OPTIONS section - used to override the
NOTE default options specified at install time
NOTE =======================================================
OPTION LIST ON
OPTION OMIT ALL BLANK LINES
OPTION PRINTER=133
NOTE =======================================================
NOTE This is the File Definitions section - the file and
NOTE fields to be used are identified here...
NOTE =======================================================
AIRPORTS: FILE DISK RECORD=80
DEF A_NAME 1-18 X 'NAME OF' 'AIRPORT'
DEF A_CITY 20-35 X 'CITY'
DEF A_COUNTRY 40-43 X 'COUNTRY' 'ABBREVIATION'
DEF A_PASS 50-57 N 'NUMBER OF' 'PASSENGERS'
PIC 'ZZ,ZZZ,ZZ9'
NOTE =======================================================
NOTE This is the GSA section - comparable to the COBOL
NOTE working-storage section
NOTE =======================================================
DEF MILLIONS_PASS (N 4.2) = NONE 'PASSENGERS' '(IN MILLIONS)'
PIC '9,999.99'
NOTE =======================================================
NOTE This is the PRESORT section - where you can specify
NOTE operations to be carried out before the data is
NOTE copied to the hit file and sorted.
NOTE =======================================================
SET MILLIONS_PASS = A_PASS / 1000000
NOTE =======================================================
NOTE This is the Report section - one or more reports
NOTE may be defined here.
NOTE =======================================================
REPORT 'WORLD''S BUSIEST AIRPORTS'
CONTROL MILLIONS_PASS DOWN
PRINT A_NAME A_CITY MILLIONS_PASS
! =======================================================
! This is the required END statement
! =======================================================
END ! This is the last statement of the request
Let's suppose that we want to report on airports in Europe only. We will need to add some selection to our request to limit the report to those airports whose country code is one of the countries in Europe. For this report, the countries to be included are Denmark, France, Germany, Italy, the Netherlands, Switzerland and the United Kingdom. We will add a series of IF-THEN-ELSE selection statements to translate the country abbreviation into a full name. The last ELSE clause will assign a special value to the country name field that is later used in the SELECT statement to omit records from the report that are not for a European airport. The revised CA Earl request follows:
NOTE =======================================================
NOTE This is the OPTIONS section - used to override the
NOTE default options specified at install time
NOTE =======================================================
OPTION LIST ON
OPTION OMIT ALL BLANK LINES
OPTION PRINTER=80
NOTE =======================================================
NOTE This is the File Definitions section - the file and
NOTE fields to be used are identified here...
NOTE =======================================================
AIRPORTS: FILE DISK RECORD=80
DEF A_NAME 1-18 X 'NAME OF' 'AIRPORT'
DEF A_CITY 20-35 X 'CITY'
DEF A_COUNTRY 40-43 X 'COUNTRY' 'ABBREVIATION'
DEF A_PASS 50-57 N 'NUMBER OF' 'PASSENGERS'
PIC 'ZZ,ZZZ,ZZ9'
NOTE =======================================================
NOTE This is the GSA section - comparable to the COBOL
NOTE working-storage section
NOTE =======================================================
DEF MILLIONS_PASS (N 4.2) = NONE 'PASSENGERS' '(IN MILLIONS)'
PIC 'Z,ZZ9.99'
DEF COUNTRY (X 20) = NONE
NOTE =======================================================
NOTE This is the PRESORT section - where you can specify
NOTE operations to be carried out before the data is
NOTE copied to the hit file and sorted.
NOTE =======================================================
!
GOTO EOJ QSEQ > 50 ! this is a conditional branch which
! limits the number of records read.
!
SET MILLIONS_PASS = A_PASS / 1000000
!
IF A_COUNTRY = 'DEN ' ! this series of
THEN SET COUNTRY = 'Denmark' ! IF-THEN-ELSE
ELSE IF A_COUNTRY = 'FRG ' ! tests could
THEN SET COUNTRY = 'Germany' ! be replaced
ELSE IF A_COUNTRY = 'FR ' ! by a DECODE
THEN SET COUNTRY = 'France' ! command
ELSE IF A_COUNTRY = 'IT '
THEN SET COUNTRY = 'Italy'
ELSE IF A_COUNTRY = 'UK '
THEN SET COUNTRY = 'United Kingdom'
ELSE IF A_COUNTRY = 'NE '
THEN SET COUNTRY = 'Netherlands'
ELSE IF A_COUNTRY = 'SW '
THEN SET COUNTRY = 'Switzerland'
ELSE SET COUNTRY = '*OMIT*' ! flag record not wanted
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF ! make sure there is an
ENDIF ! ENDIF for every IF
!
! The following GOTO signals that we are done
! processing the current record, and causes
! the current record to be tested against the
! selection criteria.
GOTO TEST
!
NOTE =======================================================
NOTE This is the Report section - one or more reports
NOTE may be defined here.
NOTE =======================================================
REPORT 'EUROPE''S BUSIEST AIRPORTS'
SELECT COUNTRY NOT = '*OMIT*'
CONTROL MILLIONS_PASS DOWN
PRINT A_NAME A_CITY COUNTRY MILLIONS_PASS
! =======================================================
! This is the required END statement
! =======================================================
END ! This is the last statement of the request
Note that we added a new field called COUNTRY to the GSA section, and changed the Picture clause for the MILLIONS_PASS field to provide suppression of leading zeros. Also, at the beginning of processing, we've added the statement GOTO EOJ QSEQ > 50. This conditional GOTO will limit the number of input records read to the first 50. For our sample AIRPORTS file this is really not necessary, but if we had a larger file we might want to use a statement such as this to limit the number of records being processed while we are testing our report. Lastly, we followed the series of IF-THEN-ELSE statements with a GOTO TEST statement. Again this statement is not necessary in this example, but its presence will allow us to create multiple presort processing sections, which is useful when a file contains multiple types of records which have different processing requirements.
For more information about writing CA Earl requests, refer to the publications, CA-Earl User Guide and CA-Earl Reference Guide.