    PROGRAM g12aafe

!      G12AAF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g12aaf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, lifreq, n, nd
       CHARACTER (1)                   :: freq
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: p(:), psig(:), t(:), tp(:)
       INTEGER, ALLOCATABLE            :: ic(:), ifreq(:), iwk(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G12AAF Example Program Results'
       WRITE (nout,*)

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

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

       IF (freq=='F' .OR. freq=='f') THEN
          lifreq = n
       ELSE
          lifreq = 0
       END IF
       ALLOCATE (p(n),psig(n),t(n),tp(n),ic(n),ifreq(lifreq),iwk(n))

!      Read in the data
       IF (lifreq==0) THEN
          READ (nin,*) (t(i),ic(i),i=1,n)
       ELSE
          READ (nin,*) (t(i),ic(i),ifreq(i),i=1,n)
       END IF

!      Calculate Kaplan-Meier statistic
       ifail = 0
       CALL g12aaf(n,t,ic,freq,ifreq,nd,tp,p,psig,iwk,ifail)

!      Display the results
       WRITE (nout,*) '  Time   Survival    Standard'
       WRITE (nout,*) '        probability  deviation'
       WRITE (nout,*)
       WRITE (nout,99999) (tp(i),p(i),psig(i),i=1,nd)

99999  FORMAT (1X,F6.1,F10.3,2X,F10.3)
    END PROGRAM g12aafe
