MainframeSupports
tip week 6/2012:

As a follow up on the tip in week 37/2011 I will show you an EDIT macro, which I have named SUMCOL and use fairly often. The idea is to create a sum of all numeric values in a selected column of data. The final sum is displayed in an ISPF message.

The EDIT macro must scan a column, which is specified as a starting and ending position. If the characters between the starting position and the ending position is a numeric value, the value is added to the sum. The lines contributing to the sum are marked using the line command *. If no * line command is specified all lines in data are used as input.

/* REXX */
ADDRESS ISREDIT
'MACRO (FRSTCOL LASTCOL) NOPROCESS'
ADDRESS ISPEXEC "CONTROL ERRORS RETURN"
COLSUM = 0
'PROCESS RANGE *'
IF RC = 0 ! RC = 4
THEN DO
  '(RGHTBND) = LRECL'
  IF ^DATATYPE(FRSTCOL, 'W')
  THEN
    FRSTCOL = 1
  ELSE
    IF FRSTCOL > RGHTBND
    THEN
      FRSTCOL = RGHTBND
    ELSE
      IF FRSTCOL < 1
      THEN
        FRSTCOL = 1
  IF ^DATATYPE(LASTCOL, 'W')
  THEN
    LASTCOL = MIN(FIRSTCOL + 9, RGHTBND)
  ELSE
    IF LASTCOL > RGHTBND
    THEN
      LASTCOL = RGHTBND
    ELSE
      IF LASTCOL < FRSTCOL
      THEN
        LASTCOL = FRSTCOL
  COLW = LASTCOL - FRSTCOL + 1
  '(THISLINE) = LINENUM .ZFRANGE'
  '(LASTLINE) = LINENUM .ZLRANGE'
  FRSTLINE = THISLINE
  DO WHILE THISLINE <= LASTLINE
    '(NEWLINE) = LINE 'THISLINE
    COLVALUE = SUBSTR(NEWLINE, FRSTCOL, COLW)
    IF DATATYPE(COLVALUE, 'N')
    THEN
      COLSUM = COLSUM + COLVALUE
    THISLINE = THISLINE + 1
  END
END
CALL ISPF_MESSAGE 'SUM = 'COLSUM, 'The sum is 'COLSUM
EXIT
ISPF_MESSAGE:
  PARSE ARG ZERRSM, ZERRLM
  ZERRALRM = 'YES'
  ZERRHM = '*'
  ADDRESS ISPEXEC "SETMSG MSG(ISRZ002)"
RETURN

The above EDIT macro makes my worklife so much easier when I have entered ISPF EDIT or VIEW on some data, where I need to calculate the sum of some numbers presented in a part of the lines or on all lines in data. The error processing may fall a little behind compared to your normal standards as most input errors will just result in a display of a sum of 0. I am pretty convinced you can do better than that.

I have often thought of a way to improve the above macro in order to make it sum many columns at the same time and to display sums after breaks in data, but I have failed so far. If you come up with a good idea or already have made something similar, I would like to hear from you.

Previous tip in english        Forrige danske tip        Tip list