楼主: wslver
1836 6

[数据管理求助] 关于reshape的数据处理 [推广有奖]

  • 1关注
  • 0粉丝

本科生

68%

还不是VIP/贵宾

-

威望
0
论坛币
1 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
453 点
帖子
19
精华
0
在线时间
181 小时
注册时间
2020-8-25
最后登录
2022-11-24

楼主
wslver 发表于 2021-1-14 22:11:31 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. clear
  2. input long Stkcd int year long ViolationID double Penalty str23 PunishmentType
  3. 405 1997  401656 500000 "2602,2604"      
  4. 405 1998  401656 500000 "2602,2604"      
  5. 405 1999  401656 500000 "2602,2604"      
  6. 405 2000  401656 500000 "2602,2604"      
  7. 407 2009   40340      . "2699"            
  8. 407 2010   40340      . "2699"            
  9. 407 2011   40340      . "2699"            
  10. 407 2011  401329      . "2699"            
  11. 407 2012  401329      . "2699"            
  12. 408 2017 4084118      . "2603"            
  13. 408 2017 4067625 600000 "2602,2604,2699"
  14. 408 2018 4084118      . "2603"            
  15. 408 2018 4067625 600000 "2602,2604,2699"
  16. 408 2019 4067625 600000 "2602,2604,2699"
  17. end
复制代码

希望得到的结果:
变量PunishmentType中,一个格子内包含了多个样本,希望将其进行拆分,并从wide变成long的形式。

本人使用的命令是:
  1. split PunishmentType ,parse(,) gen(x)
  2. destring x*,replace
  3. drop PunishmentType
  4. reshape long x,i ( ViolationID)
  5. drop if x== .
  6. rename x PunishmentType
  7. drop _j
复制代码

但结果是系统提示id变量不唯一,本人不知道问题出在哪里,求各位老师指教




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:reshape Shape RES APE Punishment

沙发
黃河泉 在职认证  发表于 2021-1-15 11:09:30
试试
  1. reshape long x, i(ViolationID year)
复制代码
看看是不是你要的?

藤椅
wslver 发表于 2021-1-16 09:46:35
黃河泉 发表于 2021-1-15 11:09
试试看看是不是你要的?
似乎还是不行
系统提示:
variable id does not uniquely identify the observations
    Your data are currently wide.  You are performing a reshape long.  You specified i(ViolationID year) and
    j(_j).  In the current wide form, variable ViolationID year should uniquely identify the observations.

我的数据中的year原本也是以2011,2012,2013的形式在一个格子中,我进行过split和reshape的整理,或许是我举例的样本太少了没有覆盖全面,但我也不知道究竟是样本中的哪一部分有问题,请问老师该怎么处理

板凳
黃河泉 在职认证  发表于 2021-1-16 10:43:03
wslver 发表于 2021-1-16 09:46
似乎还是不行
系统提示:
variable id does not uniquely identify the observations
我的意思是
  1. split PunishmentType, parse(,) gen(x)
  2. destring x*, replace
  3. drop PunishmentType
  4. reshape long x, i(ViolationID year)
  5. drop if x== .
  6. rename x PunishmentType
  7. drop _j
复制代码

报纸
zhaowill 发表于 2021-1-16 10:56:13
不太明白您的意思,不过可以试一下(如果Stkcd year可以uniquely identify the observations的话)
split PunishmentType ,parse(,) gen(punish)
destring punish*,replace
drop PunishmentType
reshape long punish, i(Stkcd year) j(PunishmentType)

地板
wslver 发表于 2021-1-16 11:20:35
黃河泉 发表于 2021-1-16 10:43
我的意思是
老师,问题解决了,是因为出现了完全重复的样本,您的命令是正确的

7
wslver 发表于 2021-1-16 11:22:33
zhaowill 发表于 2021-1-16 10:56
不太明白您的意思,不过可以试一下(如果Stkcd year可以uniquely identify the observations的话)
split  ...
似乎解决不了这个问题

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-23 22:23