2
votes

I need to compute the mean of a set of ratings (var V_Cr1 to V_Cr11) provided the relevant control variable (var targ_i.1 to targ_i.11) for each rating satisfies a condition (>=4 and not missing).

At first I attempted to adjust syntax that I found http://stackoverflow.com/questions/12711613/loop-through-items-and-sum-items-in-spss/12794702.

This is what I used:

COMPUTE TARG_VCs=0.
EXECUTE.
VECTOR TARG_I = targ_i.1 to targ_i.11.
VECTOR TARG_VC = V_Cr1 to V_Cr11.
LOOP #vecid = 1 to 11.
DO IF (TARG_I(#vecid) <=4 and not missing (TARG_VC((#vecid)))).
COMPUTE TARG_VCs= TARG_VCs+ TARG_VC(#vecid).
END IF.
END LOOP.
EXECUTE.

It seems to have worked to provide me a sum, although it returns zeroes instead of missing when conditions are not satisfied, I can work around that. However, from a sum to a valid mean I need a count of valid V_Cr that satisfy targ_i. My data has targ_i <=4 and not missing for which there is missing V_Cr (and the other way around, but that is accounted for in the above code), so a simple count won't do.

I figure I need either a straightforward way to compute the mean within a loop, a way to embed a count in the code (I've been trying that with no luck) or another way to have a valid denominator for my mean computation on the side. Any ideas?

New to SPSS syntax (heck, to any syntax), so this is seeming far more complicated to me than it probably needs to be, I am sure. TIA!

2

2 Answers

1
votes

The idea of the following approach is to first copy the variables V_Cr1 to V_Cr11 if the condition of the test variables targ_i.1 to targ_i.11 are met, otherwise the copy variable will be set to sysmis. In a second step the mean of the conditional copy variable will be computed.

* Copy v_CrX into v_Cr_ccX if targ_i.X<=4.
DO REPEAT v_Cr = v_Cr1 TO v_Cr11
         /targ_i = targ_i.1 TO targ_i.11
         /v_Cr_cc = v_CR_cc1 TO v_CR_cc.  
   IF (targ_i<=4) v_Cr_cc = v_Cr.
END REPEAT.

COMPUTE v_CR_condmean = MEAN(v_Cr_cc1 TO v_Cr_cc11).
EXECUTE.
0
votes

Why not use the mean function with COMPUTE? You can specify how many missing values to tolerate by something like mean.4(a to z).