Using the OUTTRAP Function

The OUTTRAP function puts lines of command output into a series of numbered variables, each with the same prefix. These variables save the command output and allow an exec to process the output. Specify the variable name in parentheses following the function call.
SAY 'The OUTTRAP variable name is' OUTTRAP('var')
/* Displays the variable name in which command output is trapped.  */
In this example, the variable var becomes the prefix for the numbered series of variables. Var1, var2, var3, and so on, receive a line of output each. If you do not set a limit to the number of output lines, the numbering of variables continues as long as there is output. Output from the most recent command is placed after the previous command's output. The total number of lines trapped is stored in var0.
x = OUTTRAP('var')
"LISTC"
SAY 'The number of lines trapped is' var0
To limit the number of lines of output saved, you can specify a limit, for example 5, after the variable name.
x = OUTTRAP('var',5)

This results in up to 5 lines of command output stored in var1, var2, var3, var4, var5; and var0 contains the number 5. Subsequent lines of command output are not saved.

The following example traps output from two commands and then displays the member names from a partitioned data set named MYNEW.EXEC. The stem variable includes a period, which causes the lines of output to be stored in a series of compound variables. For more information about compound variables, see Using Compound Variables and Stems.
x = OUTTRAP('var.')
"LISTC"
SAY 'The number of lines trapped is' var.0    /* could display 205 */
lines = var.0 + 1
"LISTDS mynew.exec MEMBERS"
SAY 'The number of lines trapped is' var.0    /* could display 210 */
DO i = lines TO var.0
  SAY var.i                                  /* displays 5 members */
END
To turn trapping off, reissue the OUTTRAP function with the word "OFF".
x = OUTTRAP('OFF')       /* turns trapping OFF */

The OUTTRAP function can be used only in REXX execs that run in the TSO/E address space.

The OUTTRAP function does not trap all lines of command output from all TSO/E commands. For more information, see z/OS TSO/E REXX Reference.