Creating several dummy-coded variables from a single categorical variable

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating several dummy-coded variables from a single categorical variable

tjohnson

Hello all

 

I am working with SPSS to recode some data before undertaking statistical analysis.  My problem is knowing the best/simplest way to recode my data.

 

The current data is categorical, with multiple response options, as shown below.  I would like to recode the data to create a new dummy-coded variable for each response option. 

 

Q816A1 (Q14) 1. How often do you use each of the following products?

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

1. Several times a day

119

5.3

11.7

11.7

2. Once a day

126

5.6

12.4

24.1

3. Several times a week

189

8.4

18.6

42.7

4. Once a week

99

4.4

9.7

52.5

5. Every 2-3 weeks

95

4.2

9.4

61.8

6. Once a month

69

3.1

6.8

68.6

7. A few times a year

265

11.8

26.1

94.7

8. Less than a few times a year

54

2.4

5.3

100.0

Total

1016

45.4

100.0

 

Missing

-77 System

1221

54.6

 

 

Total

2237

100.0

 

 

 

I know that I can do this manually, by specifying an IF statement for each option, but I have many variables to recode.  I am therefore wondering if anyone could help me with my syntax.

 

I have used Do Repeat before, and have been happy with the results, but I am not sure that this can be used to create several new variables from each existing variable.

 

I am sure that this can be easily done via a macro, but I have not yet mastered the art of writing SPSS macros myself!  Any help/pointers would therefore be greatly appreciated.

 

Cheers

Tim

 

Timothy Johnson | Marketing Sciences Methodologist | Harris Interactive
+44 (0)208 263 5271 |
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Creating several dummy-coded variables from a single categorical variable

Bruce Weaver
Administrator
Johnson, Timothy wrote
Hello all

I am working with SPSS to recode some data before undertaking
statistical analysis.  My problem is knowing the best/simplest way to
recode my data.

The current data is categorical, with multiple response options, as
shown below.  I would like to recode the data to create a new
dummy-coded variable for each response option.  

--- snip frequency table ---

I know that I can do this manually, by specifying an IF statement for
each option, but I have many variables to recode.  I am therefore
wondering if anyone could help me with my syntax.

 

I have used Do Repeat before, and have been happy with the results, but
I am not sure that this can be used to create several new variables from
each existing variable.

 

I am sure that this can be easily done via a macro, but I have not yet
mastered the art of writing SPSS macros myself!  Any help/pointers would
therefore be greatly appreciated.

 

Cheers

Tim

 

Timothy Johnson | Marketing Sciences Methodologist | Harris Interactive
+44 (0)208 263 5271 | tjohnson@harrisinteractive.com
<mailto:tjohnson@harrisinteractive.com> 
You could use DO-REPEAT.  E.g.,

numeric newvar1 to newvar8 (f1.0).
do repeat v = newvar1 to newvar8 / #i = 1 to 8 .
- compute v = (oldvar EQ #i).
end repeat.
exe.


Alternatively, you could use VECTOR & LOOP:

numeric newvar1 to newvar8 (f1.0).
vector v = newvar1 to newvar8 .
loop #i = 1 to 8.
- compute v(#i) = (oldvar EQ #i).
end loop.
exe.

--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Creating several dummy-coded variables from a single categorical variable

Ruben Geert van den Berg
In reply to this post by tjohnson
Hi Timothy,

The basic codes for (un)dummifying are

*Testdata, single 5-point Likert.

data list free/v1.
begin data
1 2 3 4 5
end data.

*Dummify.

do rep a=v_1 to v_5/b=1 to 5.
comp a=v1=b.
end rep.
exe.

*Undummify.

do rep a=v_1 to v_5/b=1 to 5.
if a=1 v2=b.
end rep.
exe.

HTH!

Ruben van den Berg

Methodologist

TNS NIPO

E: [hidden email]

P: +31 20 522 5738

I: www.tns-nipo.com






Date: Mon, 18 Jan 2010 17:32:25 +0000
From: [hidden email]
Subject: Creating several dummy-coded variables from a single categorical variable
To: [hidden email]

Hello all

 

I am working with SPSS to recode some data before undertaking statistical analysis.  My problem is knowing the best/simplest way to recode my data.

 

The current data is categorical, with multiple response options, as shown below.  I would like to recode the data to create a new dummy-coded variable for each response option. 

 

Q816A1 (Q14) 1. How often do you use each of the following products?

 

Frequency

Percent

Valid Percent

Cumulative Percent

Valid

1. Several times a day

119

5.3

11.7

11.7

2. Once a day

126

5.6

12.4

24.1

3. Several times a week

189

8.4

18.6

42.7

4. Once a week

99

4.4

9.7

52.5

5. Every 2-3 weeks

95

4.2

9.4

61.8

6. Once a month

69

3.1

6.8

68.6

7. A few times a year

265

11.8

26.1

94.7

8. Less than a few times a year

54

2.4

5.3

100.0

Total

1016

45.4

100.0

 

Missing

-77 System

1221

54.6

 

 

Total

2237

100.0

 

 

 

I know that I can do this manually, by specifying an IF statement for each option, but I have many variables to recode.  I am therefore wondering if anyone could help me with my syntax.

 

I have used Do Repeat before, and have been happy with the results, but I am not sure that this can be used to create several new variables from each existing variable.

 

I am sure that this can be easily done via a macro, but I have not yet mastered the art of writing SPSS macros myself!  Any help/pointers would therefore be greatly appreciated.

 

Cheers

Tim

 

Timothy Johnson | Marketing Sciences Methodologist | Harris Interactive
+44 (0)208 263 5271 |
[hidden email]



Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Re: Creating several dummy-coded variables from a single categorical variable

Jon K Peck

The recoding part of this problem could also be solved in a more general way with the SPSSINC CREATE DUMMIES extension command available from SPSS Developer Central (www.spss.com/devcentral).  That command, which requires the programmability plugin, discovers the values for a (categorical) variable and generates a set of dummy variables for them using a user-specified rootname.

Regards,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 01/18/2010 11:17 AM
Subject: Re: [SPSSX-L] Creating several dummy-coded variables from a single              categorical              variable
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi Timothy,

The basic codes for (un)dummifying are

*Testdata, single 5-point Likert.

data list free/v1.
begin data
1 2 3 4 5
end data.

*Dummify.

do rep a=v_1 to v_5/b=1 to 5.
comp a=v1=b.
end rep.
exe.

*Undummify.

do rep a=v_1 to v_5/b=1 to 5.
if a=1 v2=b.
end rep.
exe.

HTH!

Ruben van den Berg
Methodologist
TNS NIPO
E: ruben_van_den_berg@...
P: +31 20 522 5738
I: www.tns-nipo.com






Date: Mon, 18 Jan 2010 17:32:25 +0000
From: [hidden email]
Subject: Creating several dummy-coded variables from a single categorical variable
To: [hidden email]

Hello all
 
I am working with SPSS to recode some data before undertaking statistical analysis.  My problem is knowing the best/simplest way to recode my data.
 
The current data is categorical, with multiple response options, as shown below.  I would like to recode the data to create a new dummy-coded variable for each response option.  
 

Q816A1 (Q14) 1. How often do you use each of the following products?
 
Frequency
Percent
Valid Percent
Cumulative Percent
Valid 1. Several times a day
119
5.3
11.7
11.7
2. Once a day
126
5.6
12.4
24.1
3. Several times a week
189
8.4
18.6
42.7
4. Once a week
99
4.4
9.7
52.5
5. Every 2-3 weeks
95
4.2
9.4
61.8
6. Once a month
69
3.1
6.8
68.6
7. A few times a year
265
11.8
26.1
94.7
8. Less than a few times a year
54
2.4
5.3
100.0
Total
1016
45.4
100.0
 
Missing -77 System
1221
54.6
 
 
Total
2237
100.0
 
 

 
I know that I can do this manually, by specifying an IF statement for each option, but I have many variables to recode.  I am therefore wondering if anyone could help me with my syntax.
 
I have used Do Repeat before, and have been happy with the results, but I am not sure that this can be used to create several new variables from each existing variable.
 
I am sure that this can be easily done via a macro, but I have not yet mastered the art of writing SPSS macros myself!  Any help/pointers would therefore be greatly appreciated.
 
Cheers
Tim
 
Timothy Johnson | Marketing Sciences Methodologist | Harris Interactive
+44 (0)208 263 5271 |
tjohnson@...



Express yourself instantly with MSN Messenger! MSN Messenger