Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

Module 4: REXX Built-in Functions


REXX- Built-in Functions

REXX ships with roughly seventy built-in functions, and the string and word functions do the heavy lifting in most execs. Function names are case-insensitive, arguments are expressions, and every example below shows the real result. This page covers the functions you will use constantly: SUBSTR, WORD/WORDS, LENGTH, DATATYPE, POS, STRIP, TRANSLATE, DATE and TIME.

SUBSTR - extracting parts of a string

  • SUBSTR(string, n [, length [, pad]]) returns the substring starting at position n. Positions start at 1, not 0.
  • Omit length to take everything from n to the end. If n runs past the end, pad characters fill the result.
/* REXX */ SAY SUBSTR('mainframe', 5, 5) /* frame */ SAY SUBSTR('mainframe', 5) /* frame */ SAY SUBSTR('ABC', 5, 3, '*') /* *** (padded) */ rec = '10240MARGARET HOPPER DEV' SAY SUBSTR(rec, 1, 5) /* 10240 */ SAY SUBSTR(rec, 26) /* DEV */

WORD, WORDS and SUBWORD - working with words

  • REXX defines a word as text between blanks. WORDS(string) counts them, WORD(string, n) returns the nth.
  • SUBWORD(string, n [, length]) returns a run of words starting at word n.
  • Asking for a word past the end returns a null string - it never errors.
/* REXX */ line = ' one two three ' SAY WORDS(line) /* 3 */ SAY WORD(line, 2) /* two */ SAY SUBWORD(line, 2, 2) /* two three */ SAY WORD(line, 9) /* (null string) */ sentence = 'the quick brown fox' DO i = 1 TO WORDS(sentence) SAY i WORD(sentence, i) END

LENGTH, DATATYPE and POS

  • LENGTH(string) returns the character count, blanks included.
  • DATATYPE(string [, option]) tests what the string looks like: 'W' whole number, 'N' valid number, 'L' lowercase, 'U' uppercase, 'A' alphanumeric.
  • POS(needle, haystack [, start]) returns the position where needle first occurs, or 0 if it is absent.
/* REXX */ SAY LENGTH('z/OS') /* 4 */ SAY DATATYPE('123', 'W') /* 1 (whole number) */ SAY DATATYPE('12.5', 'W') /* 0 (not whole) */ SAY DATATYPE('12.5', 'N') /* 1 (valid number) */ SAY DATATYPE('abc', 'L') /* 1 (lowercase) */ SAY POS('world', 'hello world') /* 7 */ SAY POS('z', 'hello world') /* 0 (not found) */ IF POS(',', csvline) > 0 THEN SAY 'Line contains a comma'

STRIP, SPACE and TRANSLATE

  • STRIP(string [, 'B'|'L'|'T' [, char]]) removes leading/trailing/both characters - blanks by default, or any character you name.
  • SPACE(string [, n [, pad]]) normalizes blanks between words to n blanks (1 by default).
  • TRANSLATE(string) with no tables uppercases the string; with tables it maps characters one to one.
/* REXX */ SAY STRIP(' padded ') /* padded */ SAY STRIP(' padded ', 'L') /* padded (left only) */ SAY STRIP('***abc***', 'B', '*') /* abc */ SAY SPACE('a b c') /* a b c */ SAY SPACE('a b c', 2) /* a b c */ SAY TRANSLATE('MixedCase') /* MIXEDCASE */ SAY TRANSLATE('abc', 'ABC', 'abc') /* ABC */

DATE and TIME

  • DATE() defaults to the normal format like 8 Oct 2026. DATE('S') gives the sortable 20261008.
  • DATE('E') gives European 08/10/2026; DATE('U') gives USA 10/08/2026.
  • TIME() defaults to 24-hour 13:04:22. TIME('C') gives civil time like 1:04 PM.
  • Use DATE('S') for dataset qualifiers, log stamps, and anything you will sort.
/* REXX */ SAY DATE() /* 8 Oct 2026 */ SAY DATE('S') /* 20261008 */ SAY DATE('E') /* 08/10/2026 */ SAY TIME() /* 13:04:22 */ SAY TIME('C') /* 1:04 PM */ PARSE VALUE TIME() WITH hh ':' mm ':' ss stamp = DATE('S') || '_' || hh || mm || ss SAY stamp /* 20261008_130422 */

More handy functions

  • LEFT/RIGHT/CENTER pad or truncate to a fixed width - ideal for reports.
  • REVERSE, COPIES, COMPARE, VERIFY cover the remaining everyday string jobs.
  • ABS, MAX, MIN, TRUNC, FORMAT, RANDOM, SIGN cover everyday arithmetic.
  • OVERLAY, INSERT, DELSTR, CHANGESTR, COUNTSTR edit strings in place.
/* REXX */ SAY LEFT('abc', 6, '*') /* abc*** */ SAY RIGHT('abc', 6, '0') /* 000abc */ SAY CENTER('Hi', 8) /* Hi */ SAY REVERSE('abc') /* cba */ SAY COPIES('ab', 3) /* ababab */ SAY COMPARE('abc', 'abd') /* 3 (first difference) */ SAY VERIFY('12345','0123456789') /* 0 (all valid) */ SAY ABS(-5) /* 5 */ SAY MAX(3, 9, 4) /* 9 */ SAY TRUNC(3.99) /* 3 */ SAY FORMAT(3.14159, , 2) /* 3.14 */ SAY OVERLAY('XX', 'abcdef', 3) /* abXXef */ SAY CHANGESTR('l', 'hello', 'L') /* heLLo */ SAY COUNTSTR('l', 'hello') /* 2 */





© copyright mainframebug.com
Privacy Policy
MainframeBug Assistant