[EpiData-list] EpiData-list Digest, Vol 86, Issue 3

epidata-list at lists.umanitoba.ca epidata-list at lists.umanitoba.ca
Mon Dec 27 07:33:03 CST 2010


This is how I have done it in .chk

before file
  define n #.#
  define vtot ###.#
end

mean4
  before entry
    n = 0
    vtot = 0
    if var1 <> . then
      n = n+1
      vtot = vtot + var1
    endif
    if var2 <> . then
      n = n+1
      vtot = vtot + var2
    endif
    if var3 <> . then
      n = n+1
      vtot = vtot + var3
    endif
    if var4 <> . then
      n = n+1
      vtot = vtot + var4
    endif
    if n > 0 then
      mean4 = vtot / n
    else
      mean4 = .
    endif
  end
end

Use float for n and vtot or else you have to convert them before calculating mean

In Analysis, you can use the iif function to simplify this.

gen n = iif(var1<>.,1,0)+iif(var2<>.,1,0)  etc
gen vtot = iif(var1<>.,var1,0)+ etc
gen mean4 = iif(n>0,vtot/n,.)


Jamie

On 2010-12-27, Bernard wrote:

> Hello,
> 
> Thank you for your reply, but I specify my problem:
> - Variables in the questionnaire
>  Var1 ##   
>  Var2 ##
>  Var3 ##
>  Var4 ##
>  Mean4 ##.##
> 
> - In CHECK file (or ANALYSIS)
>   mean4
>   mean4 = (var1+var2+var3+var4)/4
>   end
> 
> - But sometimes, var2, for example, can fail (blank):
>  in this case, "mean4" is the sum (Var1 to var4) divided by 3!
> 
> - My question is, how to count automatically (CHECK or ANALYSIS)
>   the number of variables (var1 to var4) not missing?
> 
> Thank you for your help.



More information about the EpiData-list mailing list