我有以下数据:
+------------+---------+------+----------+-----+
| 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中做到这一点?
答案 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 |
+-----------------------------------------------------------------+