## Creating series of Dummy Variables with Syntax

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

sakejan
Posts: 2
Joined: Sat Nov 15, 2014 10:14 pm

### Creating series of Dummy Variables with Syntax

I'm doing research where I have to create a lot of dummy variables.
I tried a lot of options with loop end loop and repeat end repeat, but I'm still not where I want it to be.

What I would like to do is this:

/////
(#x) goes from 1 to 35
(#y) goes from 1 to 10

RECODE BA(#y)_CAT ((#x)=1) (ELSE=0) INTO BA(#y)_CAT(#x).
VARIABLE LABELS BA(#y)_CAT(#x) 'BA(#y)_CAT(#x)'.
EXECUTE.
/////

The problem is I don't know syntax to work with two indices (x and y) or how I can use indices in the middle of a variable name (BA(#y)_CAT(#x)). The creation of labels is not that important.

What I could come up with so far is this:

Do repeat dummy = BA1_CAT1 to BA1_CAT35
/NUMBER = 1 to 35.
if (BA1_CAT = NUMBER) dummy=1 .
if (BA1_CAT <> NUMBER) dummy = 0.
end repeat.
EXECUTE.

I have to repeat this 10 times so this saves me syntax by a factor 35 but I would like to also lose the other 10 repetitions.

I work with SPSS21 for MAC
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

### Re: Creating series of Dummy Variables with Syntax

Macro may work. Suppose your two variables are called ba and cat:

Code: Select all

``````define manyLoop ().
!do !i = 1 !to 10
!do !j = 1 !to 35
compute !concat("ba", !i, "cat", !j) = (ba = !i & cat = !j) .
!doend
!doend
!enddefine .

manyLoop .
execute .``````
sakejan
Posts: 2
Joined: Sat Nov 15, 2014 10:14 pm

### Re: Creating series of Dummy Variables with Syntax

Thank you for thinking along.

However, it is not a complete answer to my question. I do get all the variables I need, but not the correct value. In your coding the variable "ba" can have cases with different values. In my research there are different ba variables containing the cat values.

E.g. I have a variable named BA1_CAT that can have a value between 1 and 35, a variable named BA2_CAT that can have a value between 1 and 35, and so on this is something I need, because I manualy asign a given word to a numerical category. If for instance the variable BA3_CAT has the value of 22 than BA3_CAT1 to BA3CAT21 are 0, BA3_CAT22 is 1 and BA3_CAT23 to BA3_CAT35 are 0 again.

This leaves me with the problem that I want syntax to recognise a number in the middle of a variable name and create that same number in the middle of a variable along with a second one. Which now it doesn't because it expects an equal sign after an index.

### Who is online

Users browsing this forum: No registered users and 2 guests