嵌套 if 中的 SPSS 变量范围

SPSS variable scope in nested if

你会如何在 spss 中执行以下操作:

var participant_number = 0.

DO IF (condition =1 AND trial_order = 1).
    participant_number = ppnr.
    DO IF (ppnr = participant_number).
        COMPUTE start_condition = 1.
    END IF.
ELSE.
    participant_number = ppnr.
    DO IF (ppnr = participant_number).
        COMPUTE start_condition = 0.
    END IF.
END IF.

需要为内部循环定义变量participant_number,并且在整个内部if中不改变。如果参与者满足条件,我只是想为所有参与者案例设置一个值。

在 SPSS 中,通常(有例外,但让我们暂时保持简单),变量是全局的。如果它们来自数据集,则可以在语法中使用它们而不必担心超出范围。 请注意,变量需要先 "computed"/created,然后才能使用。您可以使用语法或在数据 window.

中手动执行此操作 如果要执行多个转换,

DO IF 很有用。否则,结构如

IF [condition][transformation]. EXECUTE.

会成功的。

如果我正确理解了你的目标,你可以像这样重写你的代码:

***create a temporary variable, to check each case if your condition is met. Set the temporary variable to 0 as default value.
compute tempvar=0.
***then set it to 1, if condition is met.
***This is at case level, not participant level.
if condition=1 and trial_order=1 tempvar=1.
exe.
***aggregate the temp variable, from case level at participant level.
***for each participant (ppnr), it will look at all values of tempvar, and set the start_condition as the maximum of tempvar - either 0 or 1.
AGGREGATE
  /OUTFILE=* MODE=ADDVARIABLES
  /BREAK=ppnr
  /start_condition=MAX(tempvar).

***optional.
delete variable tempvar.

最后,如果参与者的至少一个案例满足 (condition =1 AND trial_order = 1),则该参与者的每个案例的 start_condition 将为 1;否则,它将是 0。