IF Statement

Purpose

The IF statement in JCL allows for conditional execution of job steps based on the return code or completion status of previous steps. It is part of the JCL conditional processing capability that enables dynamic workflow control within a job.

Syntax

jcl
1
//[name] IF (condition) THEN

The IF statement is always followed by a THEN statement and must be terminated with an ENDIF statement. Between the THEN and ENDIF, you place the JCL statements to be executed when the condition is true.

Condition Format

The condition in an IF statement can take several forms:

  • Return code comparison: (stepname.procstepname RC comparison-operator value)
  • Abend comparison: (stepname.procstepname ABEND)
  • Specific abend code: (stepname.procstepname ABENDCC comparison-operator value)
  • Logical operators: NOT, AND, OR can combine conditions

Comparison Operators

  • GT - Greater than
  • LT - Less than
  • GE - Greater than or equal to
  • LE - Less than or equal to
  • EQ - Equal to
  • NE - Not equal to

Examples

Basic IF Statement

jcl
1
2
3
4
//STEP1 EXEC PGM=PROG1 //STEP2 IF (STEP1.RC = 0) THEN //STEP3 EXEC PGM=PROG2 // ENDIF

Execute STEP3 only if STEP1 completes with a return code of 0

Using Multiple Conditions

jcl
1
2
3
4
//STEP1 EXEC PGM=PROG1 //STEP2 IF (STEP1.RC > 4 AND STEP1.RC < 12) THEN //STEP3 EXEC PGM=ERROR1 // ENDIF

Execute the error handling program if STEP1 has a return code between 5 and 11

Checking for Abend

jcl
1
2
3
4
//STEP1 EXEC PGM=PROG1 //STEP2 IF (STEP1.ABEND) THEN //STEP3 EXEC PGM=RECOVERY // ENDIF

Execute the recovery program if STEP1 abends

With Specific Abend Code

jcl
1
2
3
4
//STEP1 EXEC PGM=PROG1 //STEP2 IF (STEP1.ABENDCC = S0C7) THEN //STEP3 EXEC PGM=DATACHECK // ENDIF

Execute the data check program if STEP1 abends with a S0C7 (data exception) code

Using NOT

jcl
1
2
3
4
//STEP1 EXEC PGM=PROG1 //STEP2 IF (NOT STEP1.RC = 0) THEN //STEP3 EXEC PGM=ERRORPROC // ENDIF

Execute the error procedure if STEP1 does not complete with a return code of 0

Complex Condition

jcl
1
2
3
4
5
//STEP1 EXEC PGM=PROG1 //STEP2 EXEC PGM=PROG2 //STEP3 IF ((STEP1.RC <= 4 AND STEP2.RC = 0) OR STEP1.ABEND) THEN //STEP4 EXEC PGM=PROG3 // ENDIF

Execute STEP4 if either (STEP1 has RC <= 4 AND STEP2 has RC = 0) OR if STEP1 abended

Notes

  • IF statements were introduced in MVS/ESA 4.3 JES2 and JES3
  • The name field on the IF statement is optional
  • Every IF must have a matching ENDIF
  • IF/THEN/ELSE/ENDIF statements can be nested up to 15 levels deep
  • The stepname referenced in a condition must be a previous step in the job
  • When using a stepname, the step must be in the same job; you cannot reference steps from included procedures
  • When referencing a step within a procedure, use the format stepname.procstepname
  • IF statements cannot appear within an in-stream procedure (between PROC and PEND)
  • IF statements cannot be continued across multiple JCL lines

Common Mistakes

  • Forgetting the ENDIF statement
  • Using invalid comparison operators (e.g., using '>' instead of 'GT')
  • Attempting to reference steps that have not yet executed
  • Complex nested IF statements that are difficult to maintain
  • Referencing a step that might be skipped by a previous conditional
  • Attempting to place IF statements within a PROC definition

Related Concepts

Related Pages