Restructure data...

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

Restructure data...

Adrian Musters
Hi all,



I've got a data set that looks like this (d1q1 is device 1, question 1 - the
numbers (1 through 12) are responses to the questions):



DATA LIST LIST /ID d1q1 d2q1 d2q2.
BEGIN DATA

1      1       2      3       4

2      5       6      7       8

3      9      10     11     12

END DATA.
LIST.



And I need to restructure it so that I have (the numbers are still the
responses, but now it's set up by ID and Device):

ID   device  q1    q2    q3.

1        1       1     2

1        2       3     4

2        1       5     6

2        2       7     8

3        1       9     10

3        2       11   12



So that each person has the different device numbers on a different row.



I thought that I could use the FLIP command to transpose everything in the
first set. and then do a bunch of add files commands. do you have any
simpler ideas?



Thanks so much in advance for your help,

Adrian
Reply | Threaded
Open this post in threaded view
|

Re: Restructure data...

Mahbub Khandoker
You can try the following syntax, yes you have to do two aggregates and one add files command!

DATA LIST LIST /ID d1q1 d1q2 d2q1 d2q2.
BEGIN DATA
1 1  2  3  4
2 5  6  7  8
3 9 10 11 12
END DATA.

COMPUTE Device = 1 .
EXECUTE .

AGGREGATE
  /OUTFILE='D:\Documents and Settings\mahbub_khandoker\Desktop\device1.sav'
  /BREAK=ID Device
  /q1 = SUM(d1q1) /q2 = SUM(d1q2).
* you need to rename all 12 variables.

COMPUTE Device = 2 .
EXECUTE .

AGGREGATE
  /OUTFILE='D:\Documents and Settings\mahbub_khandoker\Desktop\device2.sav'
  /BREAK=ID Device
  /q1 = SUM(d2q1) /q2 = SUM(d2q2).
* you need to rename all 12 variables.

GET
  FILE='D:\Documents and Settings\mahbub_khandoker\Desktop\device1.sav'.

ADD FILES /FILE=*
 /FILE='D:\Documents and Settings\mahbub_khandoker\Desktop\device2.sav'.
EXECUTE.

SORT CASES BY
  ID (A) .

Hopefully this will ease your task. Some one from this group may be come up more efficient way!
Cheers!
Mahbub



 -----Original Message-----
From:   SPSSX(r) Discussion [mailto:[hidden email]]  On Behalf Of Adrian Musters
Sent:   26-Jan-07 1:48 PM
To:     [hidden email]
Subject:             Restructure data...

Hi all,



I've got a data set that looks like this (d1q1 is device 1, question 1 - the
numbers (1 through 12) are responses to the questions):



DATA LIST LIST /ID d1q1 d2q1 d2q2.
BEGIN DATA

1      1       2      3       4

2      5       6      7       8

3      9      10     11     12

END DATA.
LIST.



And I need to restructure it so that I have (the numbers are still the
responses, but now it's set up by ID and Device):

ID   device  q1    q2    q3.

1        1       1     2

1        2       3     4

2        1       5     6

2        2       7     8

3        1       9     10

3        2       11   12



So that each person has the different device numbers on a different row.



I thought that I could use the FLIP command to transpose everything in the
first set. and then do a bunch of add files commands. do you have any
simpler ideas?



Thanks so much in advance for your help,

Adrian
Reply | Threaded
Open this post in threaded view
|

Re: Restructure data...

Hal 9000
In reply to this post by Adrian Musters
this works....





DATA LIST LIST /ID d1q1 d1q2 d2q1 d2q2.
BEGIN DATA
1 1 2 5 3 4
2 5 6 9 7 8
3 9 10 13 11 12
END DATA.
LIST.

compute device1 = 1.
compute device2 = 2.
exe.

varstocases /make device from device1 device2.
vector v(2).

define @CAT ()
!do !E = 1 !to 2
do if device = !E.
!do !F = 1 !to 2
compute !concat(v,!F) = !concat(d,!E,q,!F).
!doend
end if.
!doend
!enddefine.

@CAT.
exe.
delete variables d1q1 to d2q2.
Best,
-Gary

On 1/26/07, Adrian Musters <[hidden email]> wrote:

> Hi all,
>
>
>
> I've got a data set that looks like this (d1q1 is device 1, question 1 -
> the
> numbers (1 through 12) are responses to the questions):
>
>
>
> DATA LIST LIST /ID d1q1 d2q1 d2q2.
> BEGIN DATA
>
> 1      1       2      3       4
>
> 2      5       6      7       8
>
> 3      9      10     11     12
>
> END DATA.
> LIST.
>
>
>
> And I need to restructure it so that I have (the numbers are still the
> responses, but now it's set up by ID and Device):
>
> ID   device  q1    q2    q3.
>
> 1        1       1     2
>
> 1        2       3     4
>
> 2        1       5     6
>
> 2        2       7     8
>
> 3        1       9     10
>
> 3        2       11   12
>
>
>
> So that each person has the different device numbers on a different row.
>
>
>
> I thought that I could use the FLIP command to transpose everything in the
> first set. and then do a bunch of add files commands. do you have any
> simpler ideas?
>
>
>
> Thanks so much in advance for your help,
>
> Adrian
>
Reply | Threaded
Open this post in threaded view
|

Re: Restructure data...

Hal 9000
...improved for clarity...



DATA LIST LIST /ID d1q1 d1q2 d1q3 d1q4 d2q1 d2q2 d2q3 d2q4.
BEGIN DATA
1 1 2 5 3 4 7 5 6 4
2 5 6 9 7 8 6 4 5 3
3 9 11 13 11 12 5 3 4 2
END DATA.
LIST.

compute device1 = 1.
compute device2 = 2.

varstocases /make device from device1 device2.

vector v(4).

define @CAT ()
!do !E = 1 !to 2
do if device = !E.
!do !F = 1 !to 4
compute !concat(v,!F) = !concat(d,!E,q,!F).
!doend
end if.
!doend
!enddefine.
@CAT.
exe.

delete variables d1q1 to d2q4.
Reply | Threaded
Open this post in threaded view
|

Re: Restructure data...

Hal 9000
oops...

insert an 'exe.' after these 2 computes:

compute device1 = 1.
compute device2 = 2.