长整形

时间:2018-11-01 23:09:29

标签: stata

我有以下数据:

 CID    CATID  PRI11A   PRI12A   PRI16A   PRI17A   PRI11B   PRI12B   PRI16B   PRI17B  
583234    1      499      633      450      234      995      628      494      639  
583820    1      564      987      463      235      432      124      658       54  
583820    2      357      786      129      325       98       79      956      453  

当我运行命令时:

reshape long PRI, i(CID CATID) j(IDS PRI)

数据变化如下:

 CID     CATID  IDS   PRI11A   PRI12A   PRI16A   PRI17A   PRI11B   PRI12B   PRI16B   PRI17B   PRI  
583234     1    PRI     499      633      450      234      995      628      494      639     .  
583820     1    PRI     564      987      463      235      432      124      658       54     .  
583820     2    PRI     357      786      129      325       98       79      956      453     .  

但是,我想为CID的所有值重塑数据:

 CID     CATID  IDS   IDPRI  PRI  
583234     1     A      11   499  
583234     1     A      12   633  
583234     1     A      16   450  
583234     1     A      17   234  
583234     1     B      11   995  
583234     1     B      12   628  
583234     1     B      16   494  
583234     1     B      17   639 

如何使用reshape来做到这一点?

1 个答案:

答案 0 :(得分:2)

使用示例数据:

clear

input CID CATID PRI11A PRI12A PRI16A PRI17A PRI11B PRI12B PRI16B PRI17B
583234 1 499 633 450 234 995 628 494 639
583820 1 564 987 463 235 432 124 658 54
583820 2 357 786 129 325 98 79 956 453
end

list, sepby(CID)

     +----------------------------------------------------------------------------------------+
     |    CID   CATID   PRI11A   PRI12A   PRI16A   PRI17A   PRI11B   PRI12B   PRI16B   PRI17B |
     |----------------------------------------------------------------------------------------|
  1. | 583234       1      499      633      450      234      995      628      494      639 |
     |----------------------------------------------------------------------------------------|
  2. | 583820       1      564      987      463      235      432      124      658       54 |
  3. | 583820       2      357      786      129      325       98       79      956      453 |
     +----------------------------------------------------------------------------------------+

您首先需要reshape long作为PRI的后缀(IDS):

reshape long PRI11 PRI12 PRI16 PRI17, i(CID CATID) j(IDS "A" "B")
list, sepby(CID)

     +------------------------------------------------------+
     |    CID   CATID   IDS   PRI11   PRI12   PRI16   PRI17 |
     |------------------------------------------------------|
  1. | 583234       1     A     499     633     450     234 |
  2. | 583234       1     B     995     628     494     639 |
     |------------------------------------------------------|
  3. | 583820       1     A     564     987     463     235 |
  4. | 583820       1     B     432     124     658      54 |
  5. | 583820       2     A     357     786     129     325 |
  6. | 583820       2     B      98      79     956     453 |
     +------------------------------------------------------+

然后再次reshape long PRI

reshape long PRI, i(CID CATID IDS) j(IDPRI)
list, sepby(CID)

     +------------------------------------+
     |    CID   CATID   IDS   IDPRI   PRI |
     |------------------------------------|
  1. | 583234       1     A      11   499 |
  2. | 583234       1     A      12   633 |
  3. | 583234       1     A      16   450 |
  4. | 583234       1     A      17   234 |
  5. | 583234       1     B      11   995 |
  6. | 583234       1     B      12   628 |
  7. | 583234       1     B      16   494 |
  8. | 583234       1     B      17   639 |
     |------------------------------------|
  9. | 583820       1     A      11   564 |
 10. | 583820       1     A      12   987 |
 11. | 583820       1     A      16   463 |
 12. | 583820       1     A      17   235 |
 13. | 583820       1     B      11   432 |
 14. | 583820       1     B      12   124 |
 15. | 583820       1     B      16   658 |
 16. | 583820       1     B      17    54 |
 17. | 583820       2     A      11   357 |
 18. | 583820       2     A      12   786 |
 19. | 583820       2     A      16   129 |
 20. | 583820       2     A      17   325 |
 21. | 583820       2     B      11    98 |
 22. | 583820       2     B      12    79 |
 23. | 583820       2     B      16   956 |
 24. | 583820       2     B      17   453 |
     +------------------------------------+