I assume you are doing this in Analysis. The solution is similar for a .chk file, but the syntax is different. It is also a bit tricky doing this in a .chk file because values can be changed after the calculation is done if you are not careful.
* c is count of non-missing variables * s is sum of non-missing variables * score is final score (average of non-missing variables)
gen c=0 gen s=0 if var1 <> . then c=c+1 if var1 <> . then s=s+var1 if var2 <> . then c=c+1 if var2 <> . then s=s+var2 if var3 <> . then c=c+1 if var3 <> . then s=s+var3 if var4 <> . then c=c+1 if var4 <> . then s=s+var4 if var5 <> . then c=c+1 if var5 <> . then s=s+var5 gen score = s/c
if you do not have any defined missing values, you can also calculate c this way:
c = 5-mv(var1)-mv(var2)-mv(var3)-mv(var4)-mv(var5)
and you can also calculate s this way:
s=iif(var1<>. , var1,0)+iif(var2<>. , var2,0)+iif(var3<>. , var3,0)+iif(var4<>. , var4,0)+iif(var5<>. , var5,0)
Check the help for these functions to see how they work: mv() and iif()
Jamie
On 2013-06-13, at 3:39 AM, epidata-list@lists.umanitoba.ca wrote:
Details of my problem:
My checklist contains 5 variables. Each variable is given a score (0,1,2), but in some records, a few variables are left blank because they are not relevant. The final score is calculated by dividing the sum of score of each variable (don't take the missing into account) by the 'total number of entered variables'. id var1 var2 var3 var4 var5 1 2 1 1 0 final score = (2+1+1+0)/4 ( I use the command 'ignoremissing' to get the sum) 2 2 2 2 1 1 final score = (2+2+2+1+1)/5 3 1 1 final score=(1+1)/2