如何测试变量是否存在

时间:2018-09-26 14:10:35

标签: spss

我需要在IF语句中运行SPSS语法,该语句测试文档中是否存在变量。我无法正确进行IF测试。我正在尝试这样做:

do if (test if myVariable exists).
// execute code here
end if.
Execute.

我看过here并尝试过:

DO IF (myVariable) exist=1.
END IF.
Execute.

但是我收到错误消息“ DO IF命令上的逻辑表达式后有多余的文本。我是否误解了代码?

1 个答案:

答案 0 :(得分:3)

spssinc select variables命令根据指定的属性创建变量列表。在这种情况下,该属性将是名为“ MyVar”的变量。如果该变量不存在,则列表将为空:

spssinc select variables macroname="!findMyVar" /properties pattern="MyVar".

现在,我们定义一个仅在以上列表不为空的情况下才会运行某些命令的宏:

define doifMyVarexists ()
!if (!eval(!findMyVar)<>"") !then
  * put your commands here, like following examples.
  compute MyVar=MyVar*2.
  freq MyVar.
!ifend
!enddefine.

* the macro is ready, now we call it:
doifMyVarexists.

如果多次运行,则将遇到一个问题,如果MyVar存在一次,而在以后的运行中不存在-该列表不会被清空(仅当有变量要放入时才会被覆盖) )。 要解决此问题,请在重新运行select variables之前使用以下行清空列表:

define !findMyVar() !enddefine.
相关问题