- * Example generated by -dataex-. To install: ssc install dataex
- clear
- input str6 code int 分类 long 取值1 double 取值2
- "000001" 5015 639 .
- "000001" 5020 3871 .
- "000001" 0 7737 1
- "000001" 5025 2283 .
- "000001" 5099 895 .
- "000002" 5025 2439 .182
- "000002" 5020 8899 .664
- "000002" 5015 1595 .119
- "000002" 0 13402 1
- "000002" 5099 429 .032
- "000002" 5010 40 .003
- "000004" 5020 54 .
- "000004" 5010 4 .
- "000004" 0 284 .
- "000004" 5015 15 .
- "000005" 5020 . .52
- "000005" 0 885 .
- "000006" 0 134 .
- "000006" 5025 127 .
- "000007" 0 1087 .
- "000007" 5099 833 .7663
- "000007" 5020 254 .2337
- "000008" 0 24 .
- "000008" 5099 6 .25
- "000008" 5010 1 .0417
- "000008" 5020 9 .375
- "000008" 5025 8 .3333
- "000009" 5020 570 .134
- "000009" 5099 1990 .4678
- "000009" 5030 750 .1763
- "000009" 5015 80 .0188
- "000009" 0 4254 .
- "000009" 5025 855 .201
- "000009" 5010 9 .0021
- "000010" 0 30 .
- "000010" 5025 27 .
- "000011" 5030 1176 .
- "000011" 0 1958 .
- "000012" 5030 3783 .5453
- "000012" 0 6938 1
- "000012" 5099 3155 .4547
- "000014" 0 101 .
- "000016" 5020 1353 .
- "000016" 5015 209 .
- "000016" 5020 1130 .
- "000016" 5025 21 .
- "000016" 5020 34 .
- "000016" 0 2947 .
- "000016" 5099 15 .
- "000016" 5010 31 .
- "000016" 5010 14 .
- "000017" 0 480 .
- "000017" 5020 32 .
- "000017" 5010 6 .
- "000017" 5025 60 .
- "000019" 0 507 .
- "000019" 5099 297 .5858
- "000019" 5015 17 .0335
- "000019" 5030 45 .0888
- "000019" 5025 79 .1558
- "000019" 5020 69 .1361
- "000020" 5025 57 .13
- "000020" 5099 357 .81
- "000020" 5015 26 .06
- "000020" 0 441 .
- "000021" 5020 354 .0837
- "000021" 0 4231 .
- "000021" 5030 2088 .4935
- "000021" 5015 52 .0123
- "000021" 5025 242 .0572
- "000021" 5099 1495 .3533
- "000023" 0 264 .
- "000023" 5030 14 .
- "000023" 5025 61 .
- "000023" 5099 12 .
- "000023" 5020 66 .
- "000025" 0 976 .
- "000025" 5025 199 .
- "000025" 5099 635 .
- "000025" 5020 122 .
- "000026" 0 1614 .
- "000026" 5025 471 .
- "000027" 0 64 .
- "000028" 0 3757 .
- "000028" 5015 77 .0205
- "000028" 5099 911 .2425
- "000028" 5025 913 .243
- "000028" 5020 705 .1877
- "000028" 5030 1151 .3064
- "000029" 0 2323 .
- "000029" 5030 297 .1279
- "000029" 5020 221 .0951
- "000029" 5099 1542 .6638
- "000029" 5025 263 .1132
- "000030" 0 13 .
- "000030" 5015 2 .1538
- "000030" 5020 4 .3077
- "000030" 5025 4 .3077
- "000030" 5099 3 .2308
- "000031" 0 485 .
- end
上面的数据,因为每一个公司代码(第一列)对应的变量 “分类”包含的内容不同,比如第一个公司代码,有5个分类,第二个公司代码,只有4个分类,而且分类还和第一个公司不一样。我想把他变形成下面的理想样子
公司代码 分类1(5015) 分类2(5020) 分类3(0) 分类4(5025) 分类5(5099) 分类6(5025)
"000001" 639 3871 7737 2283 895 0
最后一列分类6其实是补齐的,因为这家代码公司下,根本没有这个类别的数据,所以补上了变量,并填上了0
使用命令reshap无法完成任务,gather和spread之类的一样不行
reshape wide 取值1 取值2,i( code ) j( 分类 )
(note: j = 0 5005 5010 5015 5020 5025 5030 5050 5060 5099)
values of variable 分类 not unique within code
Your data are currently long. You are performing a reshape wide. You specified i(code) and
j(分类). There are observations within i(code) with the same value of j(分类). In the long
data, variables i() and j() together must uniquely identify the observations.
long wide
+---------------+ +------------------+
| i j a b | | i a1 a2 b1 b2 |
|---------------| <--- reshape ---> |------------------|
| 1 1 1 2 | | 1 1 3 2 4 |
| 1 2 3 4 | | 2 5 7 6 8 |
| 2 1 5 6 | +------------------+
| 2 2 7 8 |
+---------------+
Type reshape error for a list of the problem variables.