如何测试变量是否存在

问题描述 投票:4回答:1

我需要在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命令上的逻辑表达式后面有无关的文本。我误解了代码吗?

spss
1个回答
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.
© www.soinside.com 2019 - 2024. All rights reserved.