Mainframe Path Start learning free
Core9 min readLesson 2 of 4

Decisions, loops and PARSE

REXX has the control structures you expect, plus PARSE, a powerful way to split text into pieces. Together they let you process lists of datasets, report lines and command output.

Decisions

IF and SELECT
if rc = 0 then say 'Clean'
else say 'Return code' rc

select
  when rc = 0 then status = 'OK'
  when rc <= 4 then status = 'WARNING'
  otherwise status = 'FAILED'
end

Loops

DO in its common forms
do i = 1 to 5
  say 'Line' i
end

do while more = 'YES'   /* test before each pass */
  ...
end

do forever
  if done then leave   /* LEAVE exits; ITERATE skips */
end

Stems: REXX's arrays

A stem is a variable ending in a dot, such as line.. By convention line.0 holds the count and line.1 to line.n hold the items. EXECIO and OUTTRAP fill stems in exactly this way.

PARSE: splitting text

PARSE VAR with a template
rec = 'PAYRUN01 JOB04512 CC 0008'
parse var rec jobname jobid . rc
say jobname rc        /* PAYRUN01 0008 */

dsn = 'PROD.PAY.MASTER'
parse var dsn hlq '.' rest
say hlq               /* PROD */

Words in the template take one word each; a period skips a word; a quoted string splits at that text. The last variable takes everything left over.

Useful built-in functions

FunctionExampleResult
substrsubstr('PAYROLL',1,3)PAY
pospos('.', 'A.B')2
word / wordsword('A B C',2) / words('A B C')B / 3
stripstrip(' X ')X
lengthlength('JOB')3
translatetranslate('abc')ABC
rightright(7,3,'0')007

Common mistakes

Missing END

Every DO and SELECT needs an END. A missing one gives an error far from the real mistake.

Using = to compare padded strings

= ignores leading and trailing blanks; == compares exactly. Choose deliberately.

Trusting stem.0 you never set

If you build a stem yourself, set stem.0 to the count, or loops will misbehave.

What you will see at work

Key terms

Check your understanding.
Take this lesson's quiz and save your progress. Free.

Take the lesson quiz
← Your first REXX execDatasets and TSO commands from REXX →