* MAX.CMD  Finds maximum value of given field
*
SET TALK OFF
STORE ' ' TO FIELD
STORE 0 TO MAXREPT
STORE 1 TO RECORD
?
DO WHILE T
  ACCEPT 'Enter field name (? for list)->' TO FIELD
    IF FIELD = ' '
      RELEASE FIELD,MAXREPT,RECORD
      RETURN
    ENDIF
    IF FIELD='?'
      DISP STRU
      LOOP
    ENDIF
* Check that variable exists and is not 'logical'
  IF TYPE(&FIELD)<>'N'
    IF TYPE(&FIELD)<>'C'
      DISP OFF 'Field ',FIELD,' is not in file, or is logical.'
      RELEASE FIELD,MAXREPT,RECORD
      RETURN
    ENDIF
  ENDIF
?
* Begin looking for MAX value, first guess is first value
GO TOP
DO WHILE *
  SKIP
ENDDO
STORE &FIELD TO MAX
DO WHILE .NOT. EOF
  SKIP
  IF *
    LOOP
  ENDIF
  IF &FIELD > MAX
    STORE &FIELD TO MAX
    STORE 0 TO MAXREPT
    STORE # TO RECORD
  ENDIF
  IF &FIELD = MAX
    STORE MAXREPT+1 TO MAXREPT
  ENDIF
ENDDO
*
* Display results
STORE '%' TO HARDCOPY
DO WHILE T
  DISP OFF 'Record No.',STR(RECORD,5,0),' has largest value of ',FIELD
  DO CASE
    CASE TYPE(MAX)='N'
      DISP OFF 'Value is: ',STR(MAX,12,4),'(',STR(MAXREPT,5,0),' other ';
           'occurances.)'
    CASE TYPE(MAX)='C'
      DISP OFF 'Value is: ',MAX,' (',STR(MAXREPT,5,0),' other ';
               'occurances.)'
  ENDCASE
  ?
  SET PRINT OFF
  IF HARDCOPY<>'%'
    RELEASE FIELD,MAXREPT,RECORD,MAX,HARDCOPY
    RETURN
  ENDIF
  ACCEPT 'Hardcopy (Y/N)->' TO HARDCOPY
*
* Loop for hardcopy if desired
  IF !(HARDCOPY)='Y'
    SET PRINT ON
    LOOP
  ENDIF
*
  SET TALK ON
  RELEASE FIELD,MAXREPT,RECORD,MAX,HARDCOPY
  RETURN
ENDDO
ENDDO