|
Sorin,
I've shown 2 short command sequences below for creating the dummy variables.
The number of commands required does not increase with the number of dummy
variables to be created. Your choice of the 2 methods will depend on how you
want to treat cases that are missing on the nominal variable (called CATVAR
in this example).
Method 1.
The vector command creates the new variables.
The compute command places a 1 in that element of the
vector that corresponds to the value of catvar.
One result of the recode is that cases that are missing on CATVAR will
equal 0 on all 10 dummy variables.
The vector command is described in the Syntax Reference Guide.
vector dum (10).
compute dum(catvar) = 1.
recode dum1 to dum10 (missing = 0).
execute.
Method 2.
If you want cases that are missing on catvar to be missing on dum1 to dum10,
you can use the LOOP command for similar tasks.
The commands below would perform the task with a vector and loop
combination.
LOOP is also described in the Syntax Reference Guide.
In the COMPUTE command below, if the relational expression in parentheses,
catvar=#i, is true, the result is a 1; if false, a 0.
vector dum (10).
loop #i = 1 to 10.
compute dum(#i) = (catvar=#i).
end loop.
execute.
I've added a solution from our AnswerNet below. This solution creates sets
of K-1 dummy variables from a categorical variable with K values.
The AnswerNet is found at http://www.spss.com/tech/answer/index.cfm .
David Matheson
SPSS Technical Support
Q.
What is the SPSS command to transform a nominal variable
of n classification groups into a series of n-1 indicator
(or "dummy") variables?
A.
Unfortunately, there is no single command to do this. There
are several short command sequences that can do it and
examples are provided below. Of these, the DO REPEAT
approach is somewhat more general, or at least easier if
the reference category is not the lowest value.
* creating indicator variables.
* all examples below generate indicators from a
nominal variable, called cat, that is present in
the active file.
* create 4 indicator variables for categories 1 to 4
of a 5-category variable called cat.
VECTOR nom(4).
LOOP #i = 1 to 4.
COMPUTE nom(#i) = (cat = #i).
END LOOP.
EXECUTE.
* alternatively .
* create 4 indicator variables for categories 2 to 5
of a 5-category variable called cat.
VECTOR ind(4).
LOOP #i = 1 to 4.
COMPUTE ind(#i) = (cat = #i + 1).
END LOOP.
EXECUTE.
* if you wanted to make the first category the reference
category (0 on all indicator vars) with var names reflecting
the original category : .
NUMERIC dum2 to dum5.
VECTOR dumv = dum2 to dum5.
LOOP #i = 1 to 4.
COMPUTE dumv(#i) = (cat = #i + 1).
END LOOP.
EXECUTE.
* creating similar vars as above but using do repeat command.
DO REPEAT iv = indv2 to indv5
/ c = 2 to 5 .
COMPUTE iv = (cat = c).
END REPEAT.
EXECUTE.
* if reference category were neither first nor last, but 3rd,
DO REPEAT seems handier than VECTOR and LOOP.
DO REPEAT iv = c3i1 c3i2 c3i4 c3i5 / g = 1 2 4 5 .
COMPUTE iv = (cat = g).
END REPEAT.
EXECUTE.
-----Original Message-----
From: Sorin Sion [mailto:Sorin.Sion@CONNEX.RO]
Sent: Thursday, September 02, 1999 8:04 AM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: converting nominal variables in dummy variables
Hi list!
Is there any way to convert a nominal variable with say 10 possible values,
in a range of 10 dummy variables, other than by hand?
Sorin SION
Market Research Analyst
MobiFon S.A. - ROMANIA
GSM: (+4)092.628.309
Tel: (+401)302.18.15
Fax: (+401)302.14.63
|