      SUBROUTINE RETAIN(X,N,XMIN,XMAX,NEWN)
C
C     PURPOSE--THIS SUBROUTINE RETAINS ALL OBSERVATIONS IN THE
C              SINGLE PRECISION VECTOR X WHICH ARE INSIDE
C              THE CLOSED (INCLUSIVE) INTERVAL
C              DEFINED BY XMIN AND XMAX,
C              WHILE DELETING ALL OBSERVATIONS OUTSIDE OF
C              THIS INTERVAL. 
C              THUS ALL OBSERVATIONS IN X WHICH ARE SMALLER 
C              THAN XMIN OR LARGER THAN XMAX ARE DELETED FROM X.
C              THIS SUBROUTINE (AND THE 
C              REPLAC AND DELETE SUBROUTINES)
C              GIVES THE DATA ANALYST THE ABILITY TO
C              EASILY 'CLEAN UP' A DATA SET WHICH HAS
C              MISSING AND/OR OUTLYING OBSERVATIONS
C              SO THAT A MORE APPROPRIATE SUBSEQUENT
C              DATA ANALYSIS MAY BE PERFORMED.
C              FOR EXAMPLE, A TRIMMED SAMPLE CAN EASILY
C              BE CONSTRUCTED BY USE OF THIS SUBROUTINE.
C     INPUT  ARGUMENTS--X      = THE SINGLE PRECISION VECTOR OF
C                                (UNSORTED OR SORTED) OBSERVATIONS.
C                     --N      = THE INTEGER NUMBER OF OBSERVATIONS
C                                IN THE VECTOR X. 
C                     --XMIN   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE LOWER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                INTERVAL OF INTEREST TO BE RETAINED. 
C                     --XMAX   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE UPPER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                INTERVAL OF INTEREST TO BE RETAINED. 
C     OUTPUT ARGUMENTS--NEWN   = THE INTEGER NUMBER OF OBSERVATIONS
C                                REMAINING (RETAINED) IN X AFTER ALL
C                                OF THE OBSERVATIONS OUTSIDE THE
C                                INTERVAL OF INTEREST HAVE BEEN
C                                DELETED.
C     OUTPUT--THE SINGLE PRECISION VECTOR X
C             IN WHICH ONLY THOSE VALUES INSIDE
C             (INCLUSIVELY) THE INTERVAL OF INTEREST
C             HAVE BEEN RETAINED, AND
C             THE INTEGER VALUE NEWN
C             WHICH GIVES THE NUMBER OF 
C             OBSERVATIONS RETAINED IN X.
C             ALSO, 6 LINES OF SUMMARY INFORMATION
C             WILL BE GENERATED INDICATING
C             1) WHAT THE INTERVAL OF INTEREST WAS;
C             2) HOW MANY OBSERVATIONS WERE DELETED;
C             3) WHAT THE OLD (ORIGINAL) SAMPLE SIZE WAS (N);
C             4) WHAT THE NEW SAMPLE SIZE IS (NEWN).
C     PRINTING--YES.
C     RESTRICTIONS--THERE IS NO RESTRICTION ON THE MAXIMUM VALUE
C                   OF N FOR THIS SUBROUTINE.
C     OTHER DATAPAC   SUBROUTINES NEEDED--NONE.
C     FORTRAN LIBRARY SUBROUTINES NEEDED--NONE.
C     MODE OF INTERNAL OPERATIONS--SINGLE PRECISION.
C     LANGUAGE--ANSI FORTRAN. 
C     COMMENT--IN THE END, AFTER THIS SUBROUTINE HAS
C              MADE WHATEVER DELETIONS ARE APPROPRIATE,
C              THE OUTPUT VECTOR X WILL BE 'PACKED';
C              THAT IS, NO 'HOLES' WILL EXIST IN THE
C              VECTOR X--ALL OF THE RETAINED ELEMENTS
C              OF X WILL BE PACKED INTO THE FIRST AVAILABLE 
C              LOCATIONS IN X, WHILE THE REMAINDER
C              OF THE N LOCATIONS IN X WILL BE ZERO-FILLED. 
C     COMMENT--IN THE MAIN (CALLING) ROUTINE, IT IS
C              PERMISSABLE (IF THE ANALYST SO DESIRES)
C              TO USE THE SAME VARIABLE NAME
C              IN THE FIFTH ARGUMENT AS USED IN THE SECOND
C              ARGUMENT IN THE CALLING SEQUENCE TO THIS
C              RETAIN SUBROUTINE--NO CONFLICT WILL RESULT
C              IN THE INTERNAL OPERATION OF THE     RETAIN
C              SUBROUTINE.  FOR EXAMPLE, IT IS PERMISSIBLE
C              TO HAVE     CALL RETAIN(X,N,-10.0,10.0,N)
C              IN WHICH THE VARIABLE NAME      N    IS USED 
C              AS BOTH THE SECOND AND FIFTH ARGUMENTS.
C     COMMENT--THIS IS ONE OF THE FEW SUBROUTINES IN DATAPAC
C              IN WHICH THE INPUT VECTOR X IS ALTERED.
C     REFERENCES--NONE.
C     WRITTEN BY--JAMES J. FILLIBEN
C                 STATISTICAL ENGINEERING LABORATORY (205.03)
C                 NATIONAL BUREAU OF STANDARDS
C                 WASHINGTON, D. C. 20234
C                 PHONE--301-921-2315
C     ORIGINAL VERSION--NOVEMBER  1972. 
C     UPDATED         --JULY      1974. 
C     UPDATED         --NOVEMBER  1975. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
C
      IPR=6
C
C     CHECK THE INPUT ARGUMENTS FOR ERRORS
C
      IF(N.LT.1)GOTO50
      IF(N.EQ.1)GOTO55
      HOLD=X(1)
      DO60I=2,N
      IF(X(I).NE.HOLD)GOTO90
   60 CONTINUE
      WRITE(IPR, 9)HOLD
      GOTO90
   50 WRITE(IPR,15) 
      WRITE(IPR,47)N
      RETURN
   55 WRITE(IPR,18) 
   90 CONTINUE
    9 FORMAT(1H ,108H***** NON-FATAL DIAGNOSTIC--THE FIRST  INPUT ARGUME
     1NT (A VECTOR) TO THE RETAIN SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 RETAIN SUBROUTINE IS NON-POSITIVE *****)
   18 FORMAT(1H ,100H***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUME
     1NT TO THE RETAIN SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      POINTL=XMIN
      POINTU=XMAX
      IF(XMIN.GT.XMAX)POINTL=XMAX
      IF(XMIN.GT.XMAX)POINTU=XMIN
C
      NOLD=N
      K=0 
      DO100I=1,NOLD 
      IF(X(I).LT.POINTL.OR.X(I).GT.POINTU)GOTO100 
      K=K+1
      X(K)=X(I)
  100 CONTINUE
      NEWN=K
      NDEL=NOLD-NEWN
C
      NEWNP1=NEWN+1 
      IF(NEWNP1.GT.NOLD)GOTO250
      DO200I=NEWNP1,NOLD
      X(I)=0.0
  200 CONTINUE
  250 CONTINUE
C
C     WRITE OUT A BRIEF SUMMARY
C
      WRITE(IPR,999)
      WRITE(IPR,101)
      WRITE(IPR,105)POINTL,POINTU
      WRITE(IPR,106)
      WRITE(IPR,107)
      WRITE(IPR,108)
      WRITE(IPR,110)NOLD
      WRITE(IPR,115)NEWN
      WRITE(IPR,120)NDEL
  101 FORMAT(1H ,35HOUTPUT FROM THE RETAIN SUBROUTINE--)
  105 FORMAT(1H ,7X,26HONLY OBSERVATIONS BETWEEN ,E15.8,5H AND ,E15.8)
  106 FORMAT(1H ,7X,31H(INCLUSIVE) HAVE BEEN RETAINED.)
  107 FORMAT(1H ,7X,41HALL OBSERVATIONS OUTSIDE OF THIS INTERVAL)
  108 FORMAT(1H ,7X,18HHAVE BEEN DELETED.)
  110 FORMAT(1H ,7X,44HTHE INPUT  NUMBER OF OBSERVATIONS (IN X) IS ,I6)
  115 FORMAT(1H ,7X,44HTHE OUTPUT NUMBER OF OBSERVATIONS (IN X) IS ,I6)
  120 FORMAT(1H ,7X,44HTHE NUMBER OF OBSERVATIONS DELETED       IS ,I6)
  999 FORMAT(1H )
C
      RETURN
      END 
