从长到宽重塑数据集

时间:2018-10-18 10:40:50

标签: stata

我有以下数据:

+------------+---------+------+----------+-----+
| ID  | CCID | Country | Year |   EKG    | Age |
+------------+---------+------+----------+-----+
| 101 | AA   | USA     | 2012 |  Normal  |  43 |
| 101 | AA   | USA     | 2012 |   Fail   |  12 |
| 101 | AA   | USA     | 2012 | Abnormal |  54 |
| 101 | AA   | USA     | 2013 |   Fail   |  98 |
| 101 | AA   | USA     | 2013 |  Normal  |  38 |
| 101 | AA   | USA     | 2013 | Abnormal |  29 |
+------------+---------+------+----------+-----+

我希望我的数据如下所示:

+-----+------+---------+------+----------+------+--------+
| ID  | CCID | Country | Year | Abnormal | Fail | Normal |
+-----+------+---------+------+----------+------+--------+
| 101 | AA   |  USA    | 2012 |  54      | 12   | 43     |
| 101 | AA   |  USA    | 2013 |  29      | 98   | 38     |
+-----+------+---------+------+----------+------+--------+

如何在Stata中做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用reshape命令执行此操作:

clear

input ID str2 CCID str2 Country Year str10 EKG Age
101 AA USA 2012 Normal 43
101 AA USA 2012 Fail 12
101 AA USA 2012 Abnormal 54
101 AA USA 2013 Fail 98
101 AA USA 2013 Normal 38
101 AA USA 2013 Abnormal 29
end

egen _i = group(Country CCID Year)
reshape wide Age, i(_i) j(EKG) string
drop _i

order ID CCID Country Year
list, abbreviate(15)

     +-----------------------------------------------------------------+
     |  ID   CCID   Country   Year   AgeAbnormal   AgeFail   AgeNormal |
     |-----------------------------------------------------------------|
  1. | 101     AA        US   2012            54        12          43 |
  2. | 101     AA        US   2013            29        98          38 |
     +-----------------------------------------------------------------+