    PROGRAM nag_correl_ex01

      ! Example Program Text for nag_correl
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_examples_io, ONLY : nag_std_out, nag_std_in
      USE nag_correl, ONLY : nag_prod_mom_correl, nag_part_correl
      USE nag_write_mat, ONLY : nag_write_tri_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, k, m, n, nx
      ! .. Local Arrays ..
      INTEGER, ALLOCATABLE :: index_x(:)
      REAL (wp), ALLOCATABLE :: correl(:,:), data(:,:), part_correl(:,:)
      LOGICAL, ALLOCATABLE :: var_in_x(:)
      CHARACTER (5), ALLOCATABLE :: label(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) 'Example Program Results for nag_correl_ex01'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) m, n

      ALLOCATE (data(n,m),correl(m,m)) ! Allocate storage

      ! Read data
      READ (nag_std_in,*) (data(i,:),i=1,n)

      WRITE (nag_std_out,*)

      CALL nag_prod_mom_correl(data,correl=correl)

      CALL nag_write_tri_mat('u',correl,format='f10.4',int_row_labels=.TRUE., &
       int_col_labels=.TRUE.,title='The correlation matrix for all the data')

      ! Read number of x variables
      READ (nag_std_in,*) nx

      ALLOCATE (part_correl(m-nx,m-nx),index_x(nx),var_in_x(m), &
       label(m-nx))                     ! Allocate storage

      ! Read indexes of x variables
      READ (nag_std_in,*) index_x
      var_in_x = .FALSE.
      var_in_x(index_x) = .TRUE.

      CALL nag_part_correl(correl,var_in_x,part_correl=part_correl)

      k = 0
      DO i = 1, m
        IF ( .NOT. var_in_x(i)) THEN
          k = k + 1
          WRITE (label(k),'(a2,i2,a)') 'x(', i, ')'
        END IF
      END DO
      WRITE (nag_std_out,*)

      CALL nag_write_tri_mat('u',part_correl,diag='u',format='f10.4', &
       row_labels=label,col_labels=label,title='Partial correlation matrix')

      DEALLOCATE (data,correl,part_correl,index_x,var_in_x, &
       label)                           ! Deallocate storage

    END PROGRAM nag_correl_ex01
