Variable labels in SPSS Macro

2019-08-11 03:45发布

问题:

I'm new to the SPSS macro syntax and had a hard time trying to label variables based on a simple loop counter. Here's what I tried to do:

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.

However, when I run this, I get the following warnings:

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.

Indeed the label is then only 'Indexvar has value '.

Upon using "set mprint on printback on", the following code was printed:

variable labels indexvar_value_1_ind 'Indexvar has value ' '1'

So it appears that SPSS seems to somehow remove the "+" which is supposed to concatenate the two strings, but why?

The rest of the macro worked fine, it's only the variable labels command that's causing problems.

回答1:

Try:

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

Also note:

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

Can be simplified as:

compute !indicvar = (indexvar = !i).

Where the right hand side of the COMPUTE equation evaluates to equal TRUE a 1 (one) is assigned else if FALSE a 0 (zero) is assigned. Using just a single compute in this way not only reduce the lines of code, it will also make the transformations more efficient/quicker to run.



回答2:

You might consider the SPSSINC CREATE DUMMIES extension command. It will automatically construct a set of dummies for a variable and label them with the values or value labels. It also creates a macro that lists all the variables. There is no need to enumerate the values. It creates dummies for all the values in the data.

It appears on the Transform menu as long as the Python Essentials are installed. Here is an example using the employee data.sav file shipped with Statistics.

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


标签: macros spss