Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPSS: DO REPEAT with different numbers of matched variables

Tags:

repeat

spss

I have a dataset where each case has the following set of variables:

VarA1.1 to VarA25.185 (total of 4625 variables) VarB.1 to VarB.185 (total of 185 variables)

For each case, VarA1.1, VarA2.1, VarA3.1, etc. are all linked to the same VarB.1.

I want to use a DO REPEAT function to search through each .1 instance using both VarA and VarB.

Example code:

DO REPEAT VarA = VarA1.1 to VarA25.185
/ VarB = VarB.1 to VarB.185.
if (VarA = X) AND ((VarB-Y)<0)
VarC = Z.
END REPEAT.
EXE.

However, it seems that because there are different numbers of variables in the repeat list of VarA and VarB, they don't pair up. I want to associate each VarA#(1-25).1 with VarB.1, each VarA#(1-25).2 with each VarB.2, etc. up to VarB.185 so that in the repeat function the correct pairing of variables is used.

Thanks!

like image 445
ScienceStudent Avatar asked Mar 10 '23 21:03

ScienceStudent


1 Answers

Another way to do this is to use a LOOP on the outside and a DO REPEAT on the inside. So here is some example data, with just three A variables that go to 1 to 10.

SET SEED 10.
INPUT PROGRAM.
LOOP Id = 1 TO 100.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME Sim.

*Making random data.
VECTOR A1.(10).
VECTOR A2.(10).
VECTOR A3.(10).
VECTOR B.(10).
NUMERIC X Y.
DO REPEAT a = A1.1 TO Y.
  COMPUTE a = RV.BERNOULLI(0.5).
END REPEAT.
EXECUTE.

So here is the part you want to pay attention to. Your DO REPEAT currently loops over the 25 variables. This switches it though, so the LOOP part goes over the 25 variables, but the DO REPEAT goes over each of your A vectors.

VECTOR A1 = A1.1 TO A1.10.
VECTOR A2 = A2.1 TO A2.10.
VECTOR A3 = A3.1 TO A3.10.
VECTOR B = B.1 TO B.10.
VECTOR C.(10).
LOOP #i = 1 TO 10.
  DO REPEAT A = A1 A2 A3.
    IF (A(#i) = X) AND (B(#i)-Y<0) C.(#i) = B(#i).
  END REPEAT.
END LOOP.
EXECUTE.

Code golf it is probably not going to beat the macro approach, since you have to define all of those VECTOR statements. But I think it is a conceptually clear way to write the program.

like image 96
Andy W Avatar answered Mar 12 '23 10:03

Andy W