Split and stack data

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

Split and stack data

Boreak Silk

Hello Listserv Participants,

 

I have time series data in columns, see below

 

Date

Var1

Var2

Var40

1/01/2014

58

25

 

21

2/01/2014

42

.

 

32

3/01/2014

25

.

 

24

4/01/2014

52

 

 

45

5/01/2014

36

56

 

65

:

:

:

:

:

31/03/2015

24

34

 

21

 

and wanted to turn it into rows, see below, dropping all the missing value records

 

Date

Count

Variable

1/01/2014

58

Var1

2/01/2014

42

Var1

3/01/2014

25

Var1

4/01/2014

52

Var1

5/01/2014

36

Var1

:

:

:

31/03/2015

24

Var1

1/01/2014

25

Var2

5/01/2014

56

Var2

:

:

:

31/03/2015

34

Var2

1/01/2014

21

Var40

2/01/2014

32

Var40

3/01/2014

24

Var40

4/01/2014

45

Var40

5/01/2014

65

Var40

:

:

:

31/03/2015

21

Var40

 

 

I think of splitting the data into 40 datasets and merge them using Add Cases command but I’d like to learn more syntax.

Any help?

 

 

Thank in advance.

 

 

Boreak

 

This email is intended solely for the named addressee.
If you are not the addressee indicated please delete it immediately.

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Split and stack data

Jignesh Sutar
Lookup VARSTOCASES

Solution would look something like this:

VARSTOCASES /MAKE Data FROM  Var1 to Var40 /NULL=DROP /INDEX=VarName(Data).
Reply | Threaded
Open this post in threaded view
|

Re: Split and stack data

Jignesh Sutar
In reply to this post by Boreak Silk
By the way, the format of your original data is typically refereed to as WIDE format (or even sometimes as FAT format) and when restructured as you desire, is known as LONG format. In most cases LONG format is a much preferred format to store and work with the data.
Reply | Threaded
Open this post in threaded view
|

Re: Split and stack data

David Marso
Administrator
In reply to this post by Boreak Silk
As Jignesh posted, this is a straightforward application of VARSTOCASES.
Why you would ever split the data into 40 datasets is incomprehensible.
Before VARSTOCASES we would do something like.
VECTOR vars=var1 TO var40.
LOOP varnum=1 TO 40.
COMPUTE variable=vars(I).
XSAVE OUTFILE <filespec> / KEEP ID varnum variable.
END LOOP.
EXECUTE.
GET FILE <filespec>.

This sort of code is rarely required anymore except in cases where one might want only certain rows generated conditionally based upon some computation or logical evaluation done within the loop.
Example.
setup as before...
VECTOR...
LOOP...
DO IF vars(I) LE vars(I+I) AND SUM(vars(I-1),vars(I+1)) GT 10).
COMPUTE vi=vars(I) .
COMPUTE varsii1=vars(I+I)-vars(I).
COMPUTE vargap=SUM(vars(I-1),vars(I+1)) .
XSAVE ... /KEEP ...vi varsii1 vagap ...
END IF.
END LOOP.
..........
Something which can't be done within the VARSTOCASES and might be a RPITA to do afterwards.


Boreak Silk wrote
Hello Listserv Participants,

I have time series data in columns, see below

Date

Var1

Var2

...

Var40

1/01/2014

58

25



21

2/01/2014

42

.



32

3/01/2014

25

.



24

4/01/2014

52





45

5/01/2014

36

56



65

:

:

:

:

:

31/03/2015

24

34



21


and wanted to turn it into rows, see below, dropping all the missing value records

Date

Count

Variable

1/01/2014

58

Var1

2/01/2014

42

Var1

3/01/2014

25

Var1

4/01/2014

52

Var1

5/01/2014

36

Var1

:

:

:

31/03/2015

24

Var1

1/01/2014

25

Var2

5/01/2014

56

Var2

:

:

:

31/03/2015

34

Var2

1/01/2014

21

Var40

2/01/2014

32

Var40

3/01/2014

24

Var40

4/01/2014

45

Var40

5/01/2014

65

Var40

:

:

:

31/03/2015

21

Var40



I think of splitting the data into 40 datasets and merge them using Add Cases command but I'd like to learn more syntax.
Any help?


Thank in advance.


Boreak

This email is intended solely for the named addressee.
If you are not the addressee indicated please delete it immediately.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Split and stack data

MLIves
In reply to this post by Boreak Silk

Did you look at VARSTOCASES?

Melissa

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Boreak Silk
Sent: Thursday, April 30, 2015 9:17 PM
To: [hidden email]
Subject: [SPSSX-L] Split and stack data

 

Hello Listserv Participants,

 

I have time series data in columns, see below

 

Date

Var1

Var2

Var40

1/01/2014

58

25

 

21

2/01/2014

42

.

 

32

3/01/2014

25

.

 

24

4/01/2014

52

 

 

45

5/01/2014

36

56

 

65

:

:

:

:

:

31/03/2015

24

34

 

21

 

and wanted to turn it into rows, see below, dropping all the missing value records

 

Date

Count

Variable

1/01/2014

58

Var1

2/01/2014

42

Var1

3/01/2014

25

Var1

4/01/2014

52

Var1

5/01/2014

36

Var1

:

:

:

31/03/2015

24

Var1

1/01/2014

25

Var2

5/01/2014

56

Var2

:

:

:

31/03/2015

34

Var2

1/01/2014

21

Var40

2/01/2014

32

Var40

3/01/2014

24

Var40

4/01/2014

45

Var40

5/01/2014

65

Var40

:

:

:

31/03/2015

21

Var40

 

 

I think of splitting the data into 40 datasets and merge them using Add Cases command but I’d like to learn more syntax.

Any help?

 

 

Thank in advance.

 

 

Boreak

 

This email is intended solely for the named addressee.
If you are not the addressee indicated please delete it immediately.

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD




This correspondence contains proprietary information some or all of which may be legally privileged; it is for the intended recipient only. If you are not the intended recipient you must not use, disclose, distribute, copy, print, or rely on this correspondence and completely dispose of the correspondence immediately. Please notify the sender if you have received this email in error. NOTE: Messages to or from the State of Connecticut domain may be subject to the Freedom of Information statutes and regulations.

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD