    PROGRAM g02bffe

!      G02BFF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02bff, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, ldcnt, ldrz, ldsspz, ldx,  &
                                          m, n, ncases
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: cnt(:,:), rz(:,:), sspz(:,:),        &
                                          std(:), x(:,:), xbar(:), xmiss(:)
       INTEGER, ALLOCATABLE            :: miss(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02BFF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

!      Read in the problem size
       READ (nin,*) n, m

       ldcnt = m
       ldrz = m
       ldsspz = m
       ldx = n
       ALLOCATE (cnt(ldcnt,m),rz(ldrz,m),sspz(ldsspz,m),std(m),x(ldx,m), &
          xbar(m),xmiss(m),miss(m))

!      Read in data
       READ (nin,*) (x(i,1:m),i=1,n)

!      Read in missing value flags
       READ (nin,*) miss(1:m)
       READ (nin,*) xmiss(1:m)

!      Display data
       WRITE (nout,99999) 'Number of variables (columns) =', m
       WRITE (nout,99999) 'Number of cases     (rows)    =', n
       WRITE (nout,*)
       WRITE (nout,*) 'Data matrix is:-'
       WRITE (nout,*)
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,x(i,1:m),i=1,n)
       WRITE (nout,*)

!      Calculate summary statistics
       ifail = 0
       CALL g02bff(n,m,x,ldx,miss,xmiss,xbar,std,sspz,ldsspz,rz,ldrz,ncases, &
          cnt,ldcnt,ifail)

!      Display results
       WRITE (nout,*) 'Variable   Mean    St. dev.'
       WRITE (nout,99996) (i,xbar(i),std(i),i=1,m)
       WRITE (nout,*)
       WRITE (nout,*) 'Sums of squares and cross-products about' // ' zero'
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,sspz(i,1:m),i=1,m)
       WRITE (nout,*)
       WRITE (nout,*) 'Correlation-like coefficients'
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,rz(i,1:m),i=1,m)
       WRITE (nout,*)
       WRITE (nout,99999) &
          'Minimum number of cases used for any pair of variables: ', ncases
       WRITE (nout,*)
       WRITE (nout,*) 'Numbers used for each pair are:'
       WRITE (nout,99998) (i,i=1,m)
       WRITE (nout,99997) (i,cnt(i,1:m),i=1,m)

99999  FORMAT (1X,A,I5)
99998  FORMAT (1X,6I12)
99997  FORMAT (1X,I3,3F12.4)
99996  FORMAT (1X,I5,2F11.4)
    END PROGRAM g02bffe
