* AVE.DMD Finds the average and variance of a given field
*
SET TALK OFF
*
STORE ' ' TO FIELD
STORE 0 TO NN
STORE 0 TO SMM
STORE 0 TO SSQ
*
?
DO WHILE T
  ACCEPT 'Enter field name (? for list)->' TO FIELD
    IF FIELD = ' '
      RETURN
    ENDIF
    IF FIELD = '?'
      DISPLAY STRUCTURE
      LOOP
    ENDIF
* Check that the variable type is numeric
  GO TOP
  DO WHILE TYPE(&FIELD)<>'N'
    DISPLAY OFF 'Field ',FIELD,' is not numeric or not in file.'
    RETURN
  ENDDO
?
* Collect sum and sum of squares data for field
  DO WHILE .NOT. EOF
    IF .NOT. (*)
      STORE NN+1 TO NN
      STORE (&FIELD)+SMM TO SMM
      STORE (&FIELD*&FIELD)+SSQ TO SSQ
    ENDIF
    SKIP
ENDDO
*
* Data in, do clacs
  STORE SMM/NN TO MEAN
  STORE (SSQ-(SMM*SMM)/NN)/(NN-1) TO VARIANCE
*
* Display results
  STORE '%' TO HARDCOPY
  DO WHILE T
    DISPLAY OFF 'For the field ',FIELD,' (',NN,' undeleted occurances)'
    DISPLAY OFF 'Average: ',STR(MEAN,12,4),' Variance: ',STR(VARIANCE,12,4)
    ?
    SET PRINT OFF
    IF HARDCOPY<>'%'
      RELEASE FILED,NN,SMM,SSQ,HARDCOPY,MEAN,VARIANCE
      RETURN
    ENDIF
    ACCEPT 'Hardcopy (Y/N)->' TO HARDCOPY
*
* Loop for hardcopy of output if desired
    IF !(HARDCOPY)='Y' 
      SET PRINT ON
      LOOP
    ENDIF
*
    SET TALK ON
    RELEASE FIELD,NN,SMM,SSQ,HARDCOPY,MEAN,VARIANCE
    RETURN
  ENDDO
ENDDO
