堆栈数据和重组,而不使用var到SPSS中的案例或casestovar

时间:2017-11-11 07:01:19

标签: spss

我有以下情况:一个循环(堆栈数据),只有一个索引变量,并且有多个项目对应于语句,如下图所示(抱歉它是Excel,但与SPSS中的相同): stack data - cases on multiple lines, but never filling for 1 respondent all the columns

我想达到以下情况但不使用casestovars进行重组,因为这会产生很多空变量。我记得对于旧版本,这是一个像Update这样的命令,它正在向上移动案例,以达到以下结果: reducing the cases per respondent

喜欢从这开始:

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

达到这个目的:

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

但不使用casestovars。 SPSS语法中是否有任何命令?

非常感谢,祝你有个美好的一天!

2 个答案:

答案 0 :(得分:2)

不完全确定您的数据结构可能存在多大变量,但如果您在每个响应者ID的每个q1_1 to q1_6只有一个响应的情况下进行演示,则以下内容就足够了:

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

如果有意或无意,也不确定相同受访者ID中重复索引值的重要性。

答案 1 :(得分:1)

以下语法可以完成这项工作 -

* 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.
相关问题