	subroutine mean(n,rMiss,rX,rMean,iErr)

c	Calculate the arithmetic average of a sequence of numbers.
c	Notes:
c
c	1. total number of obs must be > 1 (Note: if < 1 then iErr=1)
c	2. Input array must contain reals (e.g. rX)
c	3. Missing values (real values) are not included in calculation
c	4. If all values are missing then an error will return (e.g., iErr=1)

	real rX(1:n)

c	Initialize Variables

	iErr = 0
	iMissCount=0
	rSum = 0.0
	rCount = 0.0

c	Check and ensure there are enough to calculate.

	if (n.le.1) then
	  iErr = 1
	  return
	endif

c	Sum up and count appropriate values, otherwise count missing.

	do i=1,n
	  if (rX(i).ne.rMiss) then
	    rSum=rSum+rX(i)
	    rCount=rCount+1.0
	  else  
	    iMissCount=iMissCount+1
	  endif
	enddo

c	If all values are missing then there is no mean and alert user 
c	by sending back an error (iErr = 1). 

	if (iMissCount.eq.n) then
	  iErr = 1
	  return
	endif

c	Calculate Mean

	rMean = (rSum/rCount)

	return
	end
