    PROGRAM nag_canon_analysis_ex01

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

      ! .. Use Statements ..
      USE nag_canon_analysis, ONLY : nag_canon_var
      USE nag_examples_io, ONLY : nag_std_in, nag_std_out
      USE nag_write_mat, ONLY : nag_write_gen_mat
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND, SIZE
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, m, n, num_drop_var, v
      ! .. Local Arrays ..
      INTEGER, POINTER :: chi_df(:)
      INTEGER, ALLOCATABLE :: group(:), index_drop_var(:)
      REAL (wp), POINTER :: canon_var(:,:), chi_stat(:), loading(:,:), &
       mean_score(:,:), score(:,:), score_adjustment(:), sig_chi_stat(:)
      REAL (wp), ALLOCATABLE :: data(:,:)
      LOGICAL, ALLOCATABLE :: var_in_comp(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) &
       'Example Program Results for nag_canon_analysis_ex01'

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

      ALLOCATE (data(n,m),group(n),var_in_comp(m), &
       index_drop_var(num_drop_var)) ! Allocate storage

      NULLIFY (score,loading,canon_var,chi_stat,mean_score,score_adjustment, &
       sig_chi_stat)

      DO i = 1, n
        READ (nag_std_in,*) data(i,:), group(i)
      END DO

      READ (nag_std_in,*) index_drop_var
      var_in_comp = .TRUE.
      var_in_comp(index_drop_var) = .FALSE.

      CALL nag_canon_var(data,group,canon_var,var_in_comp=var_in_comp, &
       loading=loading,score=score,mean_score=mean_score, &
       score_adjustment=score_adjustment,chi_stat=chi_stat, &
       sig_chi_stat=sig_chi_stat,chi_df=chi_df)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) &
       'Eigenvalues   Percentage     Chisq        Sig      DF'
      WRITE (nag_std_out,*) '               variation   '

      v = SIZE(canon_var,1)
      DO i = 1, v
        WRITE (nag_std_out,'(4f12.4,i6)') canon_var(i,1), canon_var(i,2), &
         chi_stat(i), sig_chi_stat(i), chi_df(i)
      END DO

      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(loading,format='f12.4',title='Loadings')

      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(mean_score,format='f12.4', &
       title='Group mean canonical variate scores')

      WRITE (nag_std_out,*)

      CALL nag_write_gen_mat(score,format='f12.4',title= &
       'Canonical variate scores')

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) 'The score adjustment terms'
      WRITE (nag_std_out,'(10f12.4)') score_adjustment(1:v)

      DEALLOCATE (data,group,var_in_comp,score,loading,canon_var,chi_stat, &
       mean_score,score_adjustment,sig_chi_stat, &
       index_drop_var,chi_df) ! Deallocate storage

    END PROGRAM nag_canon_analysis_ex01
