Ania Ceausescu
Ania Ceausescu

Reputation: 21

stack data and restructure without using var to cases or casestovar in SPSS

I have the following situation: a loop (stack data) with only 1 index variable and with multiple items corresponding to the statements, as in the picture below (sorry it is Excel, but is the same as in SPSS): stack data - cases on multiple lines, but never filling for 1 respondent all the columns

I want to reach to the following situation but without using casestovars to restructure, because that creates a lot of empty variables. I remember for older versions it was a command like Update, which was moving up the cases, to reach the following result: reducing the cases per respondent

Like starting from this:

ID Index Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6 
1   1     1    1    
1   2                1    1
1   3                         1     1

To reach to this:

ID Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6 
1    1    1    1   1     1   1

But without using casestovars. Is there any command in SPSS syntax for this?

Thank you very much, have a nice day!

Upvotes: 2

Views: 275

Answers (2)

Jignesh Sutar
Jignesh Sutar

Reputation: 2929

Not entirely sure how variable your data structure is likely to be in reality but if as demo'ed where you have only a single response for each q1_1 to q1_6 per respondent ID, then the below would be sufficient:

dataset declare dsAgg.
aggregate outfile="dsAgg" /break=respid /q1_1 to q1_6=max(q1_1 to q1_6).

Also not sure of the significance of duplicate index values within the same respondent IDs, if this was intended or not.

Upvotes: 2

eli-k
eli-k

Reputation: 11310

The following syntax could do the job -

* first we'll recreate your example data.
data list list/respid index q1_1 to q1_6.
begin data
1,1,1,,,,,
1,2,,2,,,,
1,3,,,1,,,
1,4,,,,2,,
1,5,,,,,1,
1,6,,,,,,2
2,1,3,,,,,
2,1,,4,,,,
2,2,,,5,,,
2,2,,,,4,,
2,3,,,,,3,
2,3,,,,,,2
end data.

* now to work: first thing is to make sure the data from each ID are together.
sort cases by respid index.

* the loop will fill down the data to the last line of each ID.
do repeat qq=q1_1 to q1_6.
if respid=lag(respid) and missing(qq) qq=lag(qq).
end repeat.

* the following lines will help recognize the last line for each ID and select it.
compute lineNR=$casenum.
aggregate /outfile=* mode=ADDVARIABLES/break=respid/MXlineNR=max(lineNR).
select if lineNR=MXlineNR.
exe.

Upvotes: 1

Related Questions