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.
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.
The condition in an IF statement can take several forms:
(stepname.procstepname RC comparison-operator value)
(stepname.procstepname ABEND)
(stepname.procstepname ABENDCC comparison-operator value)
NOT
, AND
, OR
can combine conditionsGT
- Greater thanLT
- Less thanGE
- Greater than or equal toLE
- Less than or equal toEQ
- Equal toNE
- Not equal to1234//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
1234//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
1234//STEP1 EXEC PGM=PROG1 //STEP2 IF (STEP1.ABEND) THEN //STEP3 EXEC PGM=RECOVERY // ENDIF
Execute the recovery program if STEP1 abends
1234//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
1234//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
12345//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
stepname.procstepname