4

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!

ScienceStudent
  • 155
  • 1
  • 10

2 Answers2

4

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.

Andy W
  • 5,031
  • 3
  • 25
  • 51
  • Neat! Haven't ever used a `LOOP`/`DO REPEAT` combo before. Apart from having to write out all the `VECTOR` commands, that would works really well. Particularly so if you have a few or a single `A` variable as in your example and so avoids having to delve into macro language. – Jignesh Sutar Feb 14 '17 at 14:52
  • Awesome, thank you both for your answers! As someone relatively new to SPSS I do appreciate the loop/do repeat approach as it makes sense conceptually. – ScienceStudent Feb 14 '17 at 17:32
3

It looks like what you are trying to do is loop over 25 variables but repeat this for 185 variables.

It would be more intutive to use SPSS Macros to achieve this. Stepping through the below will demonstrate the building blocks for solving your data problem.

DEFINE !MyMacroName ()
SET MPRINT ON.
/* Generate some example data to match desired data format*/.

set seed = 10.
input program.
loop #i = 1 to 50.
compute case = #i.
end case.
end loop.
end file.
end input program.
dataset name sim.
execute.

!do !i =1 !to 25
  vector !concat('VarA',!i,'.(185, F1.0).').
  do repeat v = !concat('VarA',!i,'.1') to !concat('VarA',!i,'.185').
    compute v = TRUNC(RV.UNIFORM(1,6)).
  end repeat.
!doend

vector VarB.(185, F1.0).
do repeat v = VarB.1 to VarB.185.
  compute v = TRUNC(RV.UNIFORM(1,6)).
end repeat.

execute.

/* Solve actual problem */.
!do !i =1 !to 185
  !do !j = 1 !to 25
    if (!concat('VarA',!j,'.',!i) = !concat('VarB.',!i)) !concat('VarC', !j)=1.
  !doend
!doend

SET MPRINT OFF.
!ENDDEFINE.

/* Run macro */.
!MyMacroName.
Jignesh Sutar
  • 2,909
  • 10
  • 13