2

我有以下数据:

id      tests      testvalue
1       A           4
1       B           5
1       C           3
1       D           3 
2       A           3
2       B           3
3       C           3
3       D           4
4       A           3
4       B           5
4       A           1
4       B           3

我想将上述长数据格式更改为以下宽数据。

id      testA   testB    testC   testD   index
1       4      5        3         3        1
2       3      3        .         .        2
3       .      .        3         4        3
4       3      5        .         .        4
4       1      3        .         .        5

我在尝试

reshape wide testvalue, i(id) j(tests) 

它给出了错误,因为 中没有唯一值tests

这个问题的解决方案是什么?

4

1 回答 1

2

您需要创建一个额外的标识符以使复制可区分。

clear 
input id  str1    tests      testvalue
1       A           4
1       B           5
1       C           3
1       D           3 
2       A           3
2       B           3
3       C           3
3       D           4
4       A           3
4       B           5
4       A           1
4       B           3
end 
bysort id tests: gen replicate = _n 
reshape wide testvalue, i(id replicate) j(tests) string 

另请参阅此处以获取文档。

于 2015-03-03T19:36:00.227 回答