Conditional Execution (IF/THEN/ELSE/ENDIF)

Purpose

Conditional execution statements (IF/THEN/ELSE/ENDIF) allow for dynamic control of JCL execution flow based on various conditions. This powerful feature enables jobs to make runtime decisions, execute different steps based on return codes, parameter values, or dataset existence, and adapt processing logic without requiring multiple job submissions or external intervention.

Key Benefit:

Conditional execution brings programming-like logic to JCL, allowing for more intelligent and adaptable batch processing that can respond to execution conditions without operator intervention.

Basic Syntax

Simple IF Statement

jcl
1
2
3
4
// IF condition THEN //step1 EXEC PGM=program1 //step2 EXEC PGM=program2 // ENDIF

IF-ELSE Statement

jcl
1
2
3
4
5
// IF condition THEN //step1 EXEC PGM=program1 // ELSE //step2 EXEC PGM=program2 // ENDIF

Nested IF Statements

jcl
1
2
3
4
5
6
7
8
9
// IF condition1 THEN // IF condition2 THEN //step1 EXEC PGM=program1 // ELSE //step2 EXEC PGM=program2 // ENDIF // ELSE //step3 EXEC PGM=program3 // ENDIF

Condition Types

Return Code Comparison

ConditionDescriptionExample
RC = nReturn code equals nIF RC = 0 THEN
RC > nReturn code greater than nIF RC > 4 THEN
RC < nReturn code less than nIF RC < 8 THEN
RC > nReturn code greater than nIF RC > 4 THEN
RC >= nReturn code greater than or equal to nIF RC >= 4 THEN
RC <= nReturn code less than or equal to nIF RC <= 4 THEN
RC ¬= nReturn code not equal to nIF RC ¬= 0 THEN
stepname.RCReturn code from a specific stepIF STEP1.RC = 0 THEN
stepname.procstep.RCReturn code from a step within a procedureIF STEP1.PROC1.RC = 0 THEN

ABEND Condition

ConditionDescriptionExample
ABENDPrevious step abendedIF ABEND THEN
¬ABENDPrevious step did not abendIF ¬ABEND THEN
ABENDCCSystem completion codeIF ABENDCC = S0C7 THEN
stepname.ABENDSpecific step abendedIF STEP1.ABEND THEN

Run Condition

ConditionDescriptionExample
RUNPrevious step executedIF RUN THEN
¬RUNPrevious step bypassedIF ¬RUN THEN
stepname.RUNSpecific step executedIF STEP1.RUN THEN

Symbolic Parameter Comparison

ConditionDescriptionExample
&param = valueParameter equals valueIF &ENV = PROD THEN
&param != valueParameter not equal to valueIF &ENV != TEST THEN
&param > valueParameter greater than valueIF &COUNT > 100 THEN

Dataset Existence

ConditionDescriptionExample
DSN=dataset.nameDataset existsIF DSN=MY.DATASET THEN
¬DSN=dataset.nameDataset does not existIF ¬DSN=MY.DATASET THEN

Compound Conditions (AND/OR Logic)

jcl
1
2
3
// IF (STEP1.RC = 0 & STEP2.RC < 8) | STEP3.RC > 0 THEN //NEXT EXEC PGM=PROGRAM4 // ENDIF

Uses both AND (&) and OR (|) operators in a compound condition. The parentheses control precedence.

Usage Examples

Basic Return Code Checking

jcl
1
2
3
4
5
6
7
8
//RCCHECK JOB (ACCT#),'ADMIN',CLASS=A //STEP1 EXEC PGM=MYPROG //* // IF STEP1.RC = 0 THEN //STEP2 EXEC PGM=NEXTPROG // ELSE //STEPALT EXEC PGM=RECOVERY // ENDIF

This job executes NEXTPROG if MYPROG completes successfully, otherwise it runs the RECOVERY program.

Error Handling with ABEND

jcl
1
2
3
4
5
6
7
//ERRJOB JOB (ACCT#),'ADMIN',CLASS=A //STEP1 EXEC PGM=PROCESS //* // IF STEP1.ABEND THEN //CLEANUP EXEC PGM=CLEANUP //REPORT EXEC PGM=ERRRPT // ENDIF

This job runs cleanup and error reporting programs if the PROCESS program abends.

Parameter-Based Processing

jcl
1
2
3
4
5
6
7
8
//PARAMJOB JOB (ACCT#),'ADMIN',CLASS=A // SET ENV=TEST //* // IF &ENV = PROD THEN //PRODSTEP EXEC PGM=PRODPGM // ELSE //TESTSTEP EXEC PGM=TESTPGM // ENDIF

This job uses a symbolic parameter to determine which program to run based on the environment.

Dataset Existence Check

jcl
1
2
3
4
5
6
7
//DSNJOB JOB (ACCT#),'ADMIN',CLASS=A //* // IF DSN=MY.INPUT.DATASET THEN //PROCESS EXEC PGM=PROCESS // ELSE //CREATE EXEC PGM=CREATE // ENDIF

This job checks if a dataset exists before processing it, and creates it if necessary.

Multiple Return Code Checking

jcl
1
2
3
4
5
6
7
8
9
//MULTRC JOB (ACCT#),'ADMIN',CLASS=A //STEP1 EXEC PGM=PROGRAM1 //STEP2 EXEC PGM=PROGRAM2 //* // IF (STEP1.RC <= 4 & STEP2.RC <= 4) THEN //STEP3 EXEC PGM=FINALPROC // ELSE //RECOVERY EXEC PGM=RECOVERPROC // ENDIF

This job executes the final processing only if both previous steps completed with acceptable return codes.

Complex Nested Conditions

jcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//COMPLEX JOB (ACCT#),'ADMIN',CLASS=A // SET MODE=NORMAL //STEP1 EXEC PGM=PROGRAM1 //* // IF STEP1.RC < 8 THEN // IF &MODE = NORMAL THEN //NORM EXEC PGM=NORMAL // ELSE //SPEC EXEC PGM=SPECIAL // ENDIF // ELSE // IF STEP1.ABENDCC = S0C7 THEN //FIXMEM EXEC PGM=MEMFIX // ELSE //GENERAL EXEC PGM=ERRHANDLER // ENDIF // ENDIF

This example shows nested conditions that handle different return codes and abend conditions along with parameter-based logic.

Implementation Details

Scope and Limitations

  • Conditional JCL is processed by the converter before job execution
  • Maximum nesting level is usually 15 (installation-dependent)
  • IF/THEN/ELSE/ENDIF statements must begin in column 3 (after //)
  • Labels are not allowed on IF/THEN/ELSE/ENDIF statements
  • JCL conditions cannot span multiple JCL statements
  • Conditions can't be used with JES2 or JES3 control statements

Conditional Execution Within Procedures

You can use conditional statements within procedures:

jcl
1
2
3
4
5
6
7
8
9
//MYPROC PROC ENV=TEST //STEP1 EXEC PGM=PROG1 //* // IF &ENV = PROD THEN //STEP2 EXEC PGM=PRODPROG // ELSE //STEP2 EXEC PGM=TESTPROG // ENDIF // PEND

This procedure uses different programs based on the environment parameter value.

Relationship to COND Parameter

Differences between IF/THEN/ELSE and COND parameter:

IF/THEN/ELSE/ENDIFCOND Parameter
More flexible with explicit flow controlLimited to step bypass based on return codes
Can control multiple steps with one conditionApplies to individual steps only
Supports multiple condition types (ABEND, DSN, etc.)Limited to return code checking
Supports compound conditions with AND/ORLimited to OR logic via multiple conditions

Common Use Cases

Error Handling

  • Running recovery procedures after failures
  • Executing different paths based on error types
  • Creating detailed error reports for specific conditions
  • Sending notifications based on job outcomes

Environment-Specific Processing

  • Using different datasets based on environment (DEV/TEST/PROD)
  • Adjusting processing parameters by environment
  • Enabling debug output in test environments
  • Running environment-specific validation steps

Data-Dependent Processing

  • Processing only when data exists
  • Creating datasets that are missing
  • Executing different code paths based on data characteristics
  • Conditioning job flow based on data volume or content

Operational Flexibility

  • Controlling job behavior via external parameters
  • Implementing restart logic for failed jobs
  • Supporting multiple processing modes with a single job
  • Implementing decision trees for complex processing logic

Best Practices

  1. Document conditional logic with comments to explain the decision paths
  2. Indent nested conditions for better readability
  3. Limit nesting depth to maintain maintainability (3-4 levels maximum)
  4. Use symbolic parameters for condition values that might change
  5. Include error handling for all main processing paths
  6. Group related steps within conditional blocks
  7. Consider alternatives like COND parameter for simple conditions
  8. Use clear step names to make return code references more understandable

Troubleshooting

Common Issues

IssuePossible Causes & Solutions
Mismatched IF/ENDIF
  • Missing ENDIF statement
  • Extra or missing conditional statements
  • Check the nesting level and ensure each IF has a matching ENDIF
Unexpected execution path
  • Incorrect condition logic
  • Reference to wrong step name
  • Typo in symbolic parameter name
  • Use MSGLEVEL=(1,1) to see expanded JCL
JCL error in conditional statements
  • Syntax error in condition specification
  • Invalid comparison operators
  • Missing parentheses in compound conditions
  • Check JCL syntax reference for correct formatting

Debugging Tips

  • Use MSGLEVEL=(1,1) to see expanded JCL with resolved conditions
  • Add TYPRUN=SCAN to validate conditional JCL syntax without execution
  • Include debug steps that display values being tested in conditions
  • Check for correct spelling of step names in condition references
  • Review job log for condition processing messages
  • Use //JCLLIB LIST to verify procedure library resolution

JES2 vs. JES3 Considerations

Conditional JCL is supported in both JES2 and JES3 environments with minimal differences:

  • Basic IF/THEN/ELSE/ENDIF functionality is identical in both environments
  • JES3 provides additional conditional capabilities via //*PROCESS statements
  • JES3 has its own set of condition tests with the //*MAIN statement
  • JECL (Job Entry Control Language) statements are processed differently from standard JCL conditions

Related Concepts