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 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 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
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
| Function | Example | Result |
|---|---|---|
| substr | substr('PAYROLL',1,3) | PAY |
| pos | pos('.', 'A.B') | 2 |
| word / words | word('A B C',2) / words('A B C') | B / 3 |
| strip | strip(' X ') | X |
| length | length('JOB') | 3 |
| translate | translate('abc') | ABC |
| right | right(7,3,'0') | 007 |
Common mistakes
Every DO and SELECT needs an END. A missing one gives an error far from the real mistake.
= ignores leading and trailing blanks; == compares exactly. Choose deliberately.
If you build a stem yourself, set stem.0 to the count, or loops will misbehave.
What you will see at work
- PARSE turns the output of TSO commands into data you can act on — the core of most automation execs.
- Reports built in REXX are common for quick operational checks.
- Readable REXX matters: someone else will maintain it at 3 a.m.
Key terms
Check your understanding.
Take this lesson's quiz and save your progress. Free.