Example 3:

In this example, the LMDINIT service creates LISTID of the current user's data sets, and blanks out the data set name variable to prepare for the LMDLIST service. The LMDLIST service is called from within a loop, so that all data sets that match the criteria from the LMDINIT service are returned, one at a time. The LMDLIST service sets the ZDLDSORG variable, which returns PO if the data set is partitioned. For a partitioned data set, the LMINIT service is called, so that the data set can be compressed with the LMCOMP service. CONTROL ERRORS RETURN is set before the LMCOMP service so that if there is a compression error, the exec continues to run. CONTROL ERRORS CANCEL is set after the compress so that the exec halts if there is an error on any of the other services. LMFREE frees the data id, and LMDFREE frees the LISTID.

/* REXX exec to loop through a user's data sets and compress PDSes  */
address ispexec
"LMDINIT LISTID(lidv)  LEVEL("userid()")"
If rc = 0 Then
  do
    dsvar = "                ";
    keepon = "yes"
    do until keepon = "no "
      "LMDLIST LISTID("lidv") OPTION(list) dataset(dsvar) stats(yes)"
      If rc ^= 0 Then
        Do
          if rc ^= 8 then
            say "lmdlist failed with rc = " rc
          keepon = "no "
        End
      else
        do;
          if ZDLDSORG = "PO" then
            do;
              "LMINIT DATAID(daid) DATASET('"dsvar"') ENQ(EXCLU)"
              if rc = 0 then
                do
                  say "compressing "dsvar
                  "CONTROL ERRORS RETURN"
                  "LMCOMP DATAID("daid")"
                  "CONTROL ERRORS CANCEL"
                  "LMFREE DATAID("daid")"
                end
              else
                say "lminit failed with rc = "rc
            end;
        end
    end
    "LMDFREE LISTID("lidv")"
  end