Monday 15 January 2007

Cases to Variables and Variables to Cases

Some SPSS techiques are based on the assumption that cases (rows) represent independent obervations and/or that related observations are recorded in separate variables rather than separate cases. If data file contains groups of related cases, you may not be able to use the appropriate statistical techniques (Paired Samples T Test of Repeated Measures GLM) because the data are not organized in the required fashion for those techniques.

The CASETOVARS command combines the related cases and produces the new variables.
[Syntax]
GET FILE='c:\sample.sav'.
SORT CASES BY ID_number.
CASETOVARS
/ID = ID_number
/INDEX = ID_person
/SEPARATOR = "_"
/COUNT = famsize.
VARIABLE LABELS
Income_1 "30K+"
Income_2 "40K+"
Income_3 "50K+".
[/Syntax]

Sort Cases sorts the data file by the variable that will be used to group cases in CASETOVARS command. The data file must be sorted by the variable specified on the ID subcommand of the CASETOVARS command. The ID subcommand of the CASETOVARS indicates the variables that will be used to group cases together. In this example, all cases with the same value for ID_number will become a single case in the restructured file.

Optional INDEX allocate all unique values of all non-ID variables. E.g.: only values of ID_number will be used to generate new variable. SEPARATOR subcommand specifies the character that will be used to separate original variable names and the values appended to those names for the new variable names in the restructured file. By default, a period is used. The COUNT subcommand will create a new variable that indicates the number of original cases represented by each combined case in the restructured file.

VARSTOCASES command creates the exact opposite. A simple excel file contains two columns of information: income for males and income for females. There is no known or assumed relationship between male and female values that are recorded in the same row; the two columns represent independent (unrelated) observations, and we want to create cases (rows) from the columns (variables) and create new variable that indicate the gender for each case.

[Excel input data]
Male_income Female_income
123 567
234 678
345 789
456 890
[/Excel input data]

[Syntax]
GET DATA /TYPE=XLS
/FILE = 'c:\sample.xls'
/READNAMES = ON.
VARSTOCASES
/MAKE Income FROM Male_income Female_income
/INDEX = Gender
VALUE LABELS Gender 1 'Male' 2 'Female'.
[/Syntax]

The MAKE subcommand creates a sinble income variable from the two original income variables. The INDEX subcommand creates a new variable named Gender with integer values that represent the sequential order in which the original variables are specified on the MAKE subcommand.

[SPSS dataview]
Gender Income
1 123
2 567
1 234
2 678
1 345
2 789
1 456
2 890
[/SPSS dataview]

9 comments:

Anonymous said...

That's helpful, but you have a typo: the function is CASESTOVARS, not CASETOVARS.

Anonymous said...

very helpful indeed especially for my case with a database of 5000 records and 72 variables that needed to be transposed into counts. I hope you will continue to post on this blog.

Andre said...

This was really useful in helping me remember the precise format with a nice simple example. The SPSS Data/Restructure/Variables to Cases pull-downs didn't help at all! Thanks for this post

Anonymous said...

Bridal shoes can be Hogan
they can come is made of talent to look out for the wedding day. This is durability, second is the Hogan uomo
size of wardrobe. You can cast on a V-cut black leotard, black tiny-skirt, black stockings and hogan donna
, and they have traditionally been struggling to put Hogan scarpe uomo
on the wall of oven.

Anonymous said...

Bridal shoes can be Hogan
they can come is made of talent to look out for the wedding day. This is durability, second is the Hogan uomo
size of wardrobe. You can cast on a V-cut black leotard, black tiny-skirt, black stockings and hogan donna
, and they have traditionally been struggling to put Hogan scarpe uomo
on the wall of oven.

Liz Castillo said...

Hi! this was very helpful, but when i tried to adapt it for what i was needed it doesn´t work xD! I want to make several "groups" but it keeps sending me this error:

Subcommand INDEX was specified more than once

my syntax:

VARSTOCASES
/MAKE P02_AntesD FROM P02A_1 P02A_2 P02A_3 P02A_4 P02A_5 P02A_6 P02A_7 P02A_8 P02A_9
/INDEX=ProductA(9)
/MAKE P03_AntesD FROM P03A_1 P03A_2 P03A_3 P03A_4 P03A_5 P03A_6 P03A_7 P03A_8 P03A_9
/INDEX=MARCAA(9)

i'm so new at SPSS x_x but i know about programming.. ihope you can light my way .. :D

ugg women said...

Well worth to read this article, thanks for sharing this information. With this article you offered me got a chance to know about this, anyway i say Great Article! and waiting for you next article about this interesting subject.

Anonymous said...

/INDEX = Gender

should be:

/INDEX = Gender(2)

Cheap jordan said...

It is getting cold out so bundle up with some sports Winter Wear. We have Scarves, Hats, Gloves and a Ceramic Travel Mug to keep your Hot Tea, Hot Coco or Coffee in. Check out the webstore HERE!
[url=http://www.handbags-zone.com]sell coach bags,buy designer handbags[/url]
[url=http://www.wearfoot.com]CK men boxers,CK underwear[/url]
[url=http://www.buyinb2c.com]burberry scarves,cheap burberry scarves,only 18USD[/url]