SPSS宏中的变量标签

时间:2015-08-14 16:22:06

标签: macros spss

我是SPSS宏语法的新手,并且很难尝试基于简单的循环计数器标记变量。这就是我试图做的事情:

define !make_indicatorvars()

  !do !i = 1 !to 10.

    !let !indicvar = !concat('indexvar_value_', !i, '_ind')

    compute !indicvar = 0.
    if(indexvar = !i) !indicvar = 1.

    variable labels !indicvar 'Indexvar has value ' + !quote(!i).
    value labels !indicvar 0 "No" 1 "Yes".

  !doend

!enddefine.

然而,当我运行它时,我收到以下警告:

Warning # 207 on line ... in column ... Text: ...
A '+' was found following a text string, indicating continuation, but the next non-blank character was not a quotation mark or an apostrophe.

Warning # 4465 in column ...  Text: ...
An invalid symbol appears on the VAR LABELS command where a slash was
expected.  All text through the next slash will be be ignored.

事实上,标签只是' Indexvar有价值'。

使用"在"上打印回版时设置mprint,打印以下代码:

variable labels indexvar_value_1_ind 'Indexvar has value ' '1'

所以似乎SPSS似乎以某种方式删除了" +"应该连接两个字符串,但为什么?

宏的其余部分工作得很好,它只是变量标签命令导致问题。

2 个答案:

答案 0 :(得分:2)

尝试:

variable labels !indicvar !quote(!concat('Indexvar has value ',!i)).

另请注意:

compute !indicvar = 0.
if(indexvar = !i) !indicvar = 1.

可以简化为:

compute !indicvar = (indexvar = !i).

如果COMPUTE方程的右侧评估为等于TRUE,则分配1(一),如果FALSE分配了0(零),则为其分配。以这种方式使用单个计算不仅可以减少代码行数,还可以使转换更有效/更快地运行。

答案 1 :(得分:1)

您可以考虑使用SPSSINC CREATE DUMMIES扩展命令。它将自动为变量构造一组虚拟变量,并使用值或值标签对其进行标注。它还会创建一个列出所有变量的宏。无需枚举值。它为数据中的所有值创建虚拟对象。

只要安装了Python Essentials,它就会出现在Transform菜单上。以下是使用Statistics附带的employee data.sav文件的示例。

SPSSINC CREATE DUMMIES VARIABLE=jobcat 
ROOTNAME1=job 
/OPTIONS ORDER=A USEVALUELABELS=YES USEML=YES OMITFIRST=NO
MACRONAME1="!jobcat".
相关问题