2015-08-14 73 views
1

我是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上printback”,下面的代码被印:

variable labels indexvar_value_1_ind 'Indexvar has value ' '1' 

所以看来SPSS似乎以某种方式删除“+”这是应该来连接两个字符串,但为什么?

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

回答

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 (1)如果FALSE a 0(零)为签。以这种方式使用单个计算器不仅可以减少代码行,还可以使转换更高效/更快地运行。

1

您可能会考虑使用SPSSINC CREATE DUMMIES扩展命令。它会自动为一个变量构造一组假设,并用值或值标签给它们加上标签。它还创建一个列出所有变量的宏。没有必要枚举这些值。它为数据中的所有值创建虚拟变量。

只要安装了Python Essentials,它就会显示在Transform菜单上。以下是使用统计信息附带的员工data.sav文件的示例。

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