Home > Software engineering >  How can you run a loop in SPSS syntax that calculates the difference between many sets of variables?
How can you run a loop in SPSS syntax that calculates the difference between many sets of variables?

Time:11-20

I have a set of variables (A1, A2, B1, B2, C1, C3 ...) that I need to calculate the difference for to eventually create a set of Bland-Altman plots after extracting the mean difference and sd of the difference from a t-test using OMS.

As a first step I have it working for a single pair of variables (e.g. A1 and A2) and am now trying to create a macro that will loop through the first few pairs as a test:

```
DEFINE BlandAlt (scan1vars=!CMDEND / scan2vars=!CMDEND) 
COMPUTE diff = scan1vars - scan2vars.
EXECUTE.

T-TEST
/TESTVAL=0
/MISSING=ANALYSIS
/VARIABLES=diff
/CRITERIA=CI(.95).

 !ENDDEFINE.

BlandAlt 
scan1vars = JumpJumpHeightcm.1 JumpJumpHeightt_score.1 JumpMaxChangeinAccelerationms3.1 JumpMaxChangeinAccelerationt_score.1 JumpMaxAccelerationms2.1 JumpMaxAccelerationt_score.1 
scan2vars= JumpJumpHeightcm.2 JumpJumpHeightt_score.2  JumpMaxChangeinAccelerationms3.2  JumpMaxChangeinAccelerationt_score.2  JumpMaxAccelerationms2.2 JumpMaxAccelerationt_score.2.
```

When I run the macro I get an error on the first variable:

Error # 4381 in column 35. Text: JumpJumpHeightt_score.1 The expression ends unexpectedly. Execution of this command stops.

and a warning when it tries to run the t-test:

Text: diff Command: T-TEST An undefined variable name, or a scratch or system variable was specified in a variable list >which accepts only standard variables. Check spelling and verify the existence of this variable. Execution of this command stops.

Is anyone able to help get this part working? I'm hoping it should then be easy to include the other commands within the macro.

CodePudding user response:

I have another solution to offer - putting this in a separate answer because it uses a completely different approach. The idea is that sometimes instead of looping through variables for analyses, you can get the same results by restructuring to long format and then analysing only once using split file. Like this:

varstocases 
  make scan1vars from JumpJumpHeightcm.1 JumpJumpHeightt_score.1 JumpMaxChangeinAccelerationms3.1
     JumpMaxChangeinAccelerationt_score.1 JumpMaxAccelerationms2.1 JumpMaxAccelerationt_score.1 /
  make scan2vars from JumpJumpHeightcm.2 JumpJumpHeightt_score.2  JumpMaxChangeinAccelerationms3.2
     JumpMaxChangeinAccelerationt_score.2  JumpMaxAccelerationms2.2 JumpMaxAccelerationt_score.2 /
  index=origvar(scan1vars).

* now you can do the whole process only once.
COMPUTE diff = scan1vars - scan2vars.
sort cases by origvar.
split file by origvar.
T-TEST
/TESTVAL=0
/MISSING=ANALYSIS
/VARIABLES=diff
/CRITERIA=CI(.95).
split file off.

CodePudding user response:

See my comment for corrections to your original macro. After correcting the macru it should work well, only you are not using the macro call the way it is built. You need to call it this way:

BlandAlt scan1vars = JumpJumpHeightcm.1 / scan2vars= JumpJumpHeightcm.2 .
BlandAlt scan1vars = JumpJumpHeightt_score.1 / scan2vars=JumpJumpHeightt_score.2 .
...

Now this is obviously not looping throug your variable list. The problem with SPSS macro is that it's very difficult to get it to loop through two lists at the same time. But in your case, there is no need - there is only one actual list to loop through, while letting the macro add the 1 or 2 suffix to the variable name. Try this:

DEFINE BlandAlt (vrs=!CMDEND) 
!do !vr !in(!vrs)
COMPUTE diff = !concat(!vr,".1") - !concat(!vr,".2").
EXECUTE.
TTEST.....
!doend
!enddefine.

Now the macro call would look like this:

BlandAlt vrs = JumpJumpHeightcm JumpJumpHeightt_score JumpMaxChangeinAccelerationms3  
  JumpMaxChangeinAccelerationt_score JumpMaxAccelerationms2 JumpMaxAccelerationt_score .
  • Related