2015-08-14 75 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".