如何测试变量是否存在
How to test if a variable exists
我需要 运行 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 命令的逻辑表达式后有无关文本。我误解了代码吗?
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 存在一次而稍后 运行 不存在 - 列表不会被清空(它是只有在有变量要放入其中时才会被覆盖)。
要解决此问题,请使用以下行在再次 运行ning select variables
之前清空列表:
define !findMyVar() !enddefine.
我需要 运行 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 命令的逻辑表达式后有无关文本。我误解了代码吗?
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 存在一次而稍后 运行 不存在 - 列表不会被清空(它是只有在有变量要放入其中时才会被覆盖)。
要解决此问题,请使用以下行在再次 运行ning select variables
之前清空列表:
define !findMyVar() !enddefine.