楼主: yihenglu
1315 2

[数据管理求助] 如何将带有逗号的数据拆分并形成新的数据行? [推广有奖]

  • 0关注
  • 0粉丝

硕士生

7%

还不是VIP/贵宾

-

威望
0
论坛币
12 个
通用积分
0.0792
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
706 点
帖子
35
精华
0
在线时间
230 小时
注册时间
2018-1-7
最后登录
2025-1-19

楼主
yihenglu 学生认证  发表于 2021-10-20 22:15:22 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
原始数据如下:
  1. [/size]
  2. [size=14px]* Example generated by -dataex-. To install: ssc install dataex[/size]
  3. [size=14px]clear[/size]
  4. [size=14px]input str12 symbol str89 violationyear[/size]
  5. [size=14px]"000004" "2010,2011,2012,2013"               [/size]
  6. [size=14px]"000009" "2016"                              [/size]
  7. [size=14px]"000010" "2003,2004,2005,2006,2007,2008,2009"[/size]
  8. [size=14px]"000010" "2003,2005,2007,2009,2010,2011"     [/size]
  9. [size=14px]"000010" "2015,2016"                         [/size]
  10. [size=14px]"000010" "2015,2016"                         [/size]
  11. [size=14px]"000012" "2013,2014,2015,2016"               [/size]
  12. [size=14px]"000014" "2004,2005,2006,2007,2008"          [/size]
  13. [size=14px]"000020" "2000,2001,2004"                    [/size]
  14. [size=14px]"000026" "2006,2007,2008,2009"               [/size]
  15. [size=14px]"000031" "2007,2009"                         [/size]
  16. [size=14px]"000036" "2009,2010,2011,2012"               [/size]
  17. [size=14px]"000040" "2010,2011"                         [/size]
  18. [size=14px]"000045" "2007,2008,2009"                    [/size]
  19. [size=14px]"000048" "2008,2009,2010,2011,2012"          [/size]
  20. [size=14px]"000069" "2008,2009,2010"                    [/size]
  21. [size=14px]"000078" "2017,2018,2019"                    [/size]
  22. [size=14px]"000100" "2011,2012"                         [/size]
  23. [size=14px]"000150" "2016,2017,2018,2019"               [/size]
  24. [size=14px]"000153" "2006"                              [/size]
  25. [size=14px]"000155" "2010,2011,2012"                    [/size]
  26. [size=14px]"000156" "2002,2003,2004"                    [/size]
  27. [size=14px]"000400" "2000,2001"                         [/size]
  28. [size=14px]"000400" "2001,2002"                         [/size]
  29. [size=14px]"000403" "2001,2002"                         [/size]
  30. [size=14px]"000403" "2018"                              [/size]
  31. [size=14px]"000405" "1997,1998,1999,2000"               [/size]
  32. [size=14px]"000407" "2009,2010,2011"                    [/size]
  33. [size=14px]"000407" "2011,2012"                         [/size]
  34. [size=14px]"000408" "2017,2018,2019"                    [/size]
  35. [size=14px]"000408" "2017,2018,2019,2020"               [/size]
  36. [size=14px]"000409" "2005,2006,2007,2008,2009"          [/size]
  37. [size=14px]"000410" "2017,2018,2019"                    [/size]
  38. [size=14px]"000413" "2006,2007,2008,2009"               [/size]
  39. [size=14px]"000420" "2008,2009,2010,2011,2013"          [/size]
  40. [size=14px]"000428" "2011"                              [/size]
  41. [size=14px]"000430" "1996,1997,1998"                    [/size]
  42. [size=14px]"000488" "2016,2017"                         [/size]
  43. [size=14px]"000488" "2016,2017,2018"                    [/size]
  44. [size=14px]"000498" "2011,2013,2014,2015,2016"          [/size]
  45. [size=14px]"000503" "2000,2001,2002"                    [/size]
  46. [size=14px]"000504" "2012,2013"                         [/size]
  47. [size=14px]"000504" "2012,2013"                         [/size]
  48. [size=14px]"000506" "2016,2017,2018"                    [/size]
  49. [size=14px]"000509" "2000,2001,2002"                    [/size]
  50. [size=14px]"000510" "1997,1998,1999"                    [/size]
  51. [size=14px]"000511" "2016"                              [/size]
  52. [size=14px]"000511" "2015,2016"                         [/size]
  53. [size=14px]"000511" "2016,2017"                         [/size]
  54. [size=14px]"000511" "2015,2016"                         [/size]
  55. [size=14px]"000513" "2009,2012,2013"                    [/size]
  56. [size=14px]"000514" "1998,1999,2000"                    [/size]
  57. [size=14px]"000514" "2011"                              [/size]
  58. [size=14px]"000516" "2007,2008,2009,2010,2011"          [/size]
  59. [size=14px]"000519" "2015,2016,2017"                    [/size]
  60. [size=14px]"000519" "2015,2016,2017"                    [/size]
  61. [size=14px]"000529" "2003,2004"                         [/size]
  62. [size=14px]"000533" "2010,2011,2012"                    [/size]
  63. [size=14px]"000534" "2010,2012,2013"                    [/size]
  64. [size=14px]"000534" "2010,2012,2013"                    [/size]
  65. [size=14px]"000536" ""                                  [/size]
  66. [size=14px]"000540" "2000,2001,2002,2003,2004"          [/size]
  67. [size=14px]"000543" "2012"                              [/size]
  68. [size=14px]"000545" "2012"                              [/size]
  69. [size=14px]"000549" "2003,2004"                         [/size]
  70. [size=14px]"000549" "1997,1998,1999,2000,2003,2004"     [/size]
  71. [size=14px]"000557" "1995,1997,1998,1999,2000,2001"     [/size]
  72. [size=14px]"000559" "1996,2000,2001"                    [/size]
  73. [size=14px]"000565" "2011,2012,2013,2014,2015"          [/size]
  74. [size=14px]"000566" "2014,2015,2016,2017"               [/size]
  75. [size=14px]"000566" "2015,2016,2017,2018"               [/size]
  76. [size=14px]"000566" "2015,2016,2017,2020"               [/size]
  77. [size=14px]"000570" "1994,2000,2001"                    [/size]
  78. [size=14px]"000573" "2009"                              [/size]
  79. [size=14px]"000576" "2015,2018,2019"                    [/size]
  80. [size=14px]"000587" "2018,2019"                         [/size]
  81. [size=14px]"000590" "2005,2006,2007,2008"               [/size]
  82. [size=14px]"000590" "2005,2006,2007,2008"               [/size]
  83. [size=14px]"000590" "2005,2006,2007,2008"               [/size]
  84. [size=14px]"000591" "2016,2017"                         [/size]
  85. [size=14px]"000592" "2008,2010,2009,2011"               [/size]
  86. [size=14px]"000594" "2009,2010,2011,2012"               [/size]
  87. [size=14px]"000595" "1996,1997,1998,1999,2000,2001"     [/size]
  88. [size=14px]"000597" "2012,2013"                         [/size]
  89. [size=14px]"000597" "2012"                              [/size]
  90. [size=14px]"000603" "2003,2004,2005,2006,2007,2008"     [/size]
  91. [size=14px]"000605" "2014,2015"                         [/size]
  92. [size=14px]"000605" "2009,2010"                         [/size]
  93. [size=14px]"000610" "2011,2012,2013"                    [/size]
  94. [size=14px]"000611" "2011,2012"                         [/size]
  95. [size=14px]"000612" "2008,2009,2010"                    [/size]
  96. [size=14px]"000613" "1993,1994,1995,1996,1997"          [/size]
  97. [size=14px]"000628" "2009"                              [/size]
  98. [size=14px]"000628" "2005,2006,2007,2008"               [/size]
  99. [size=14px]"000628" "2007,2008,2009"                    [/size]
  100. [size=14px]"000632" "2011,2012,2013,2014"               [/size]
  101. [size=14px]"000635" "1997,1998,1999,2000"               [/size]
  102. [size=14px]"000636" "2016,2017,2018"                    [/size]
  103. [size=14px]"000636" "2016"                              [/size]
  104. [size=14px]"000637" "2014,2015"                         [/size]
  105. [size=14px]end[/size]
  106. [size=14px]
复制代码




希望形成的数据格式如下:
symbol        violationyear
000004        2010
000004        2011
000004        2012
000004        2013
000009        2016
000010        2003
000010        2004
000010        2005
000010        2006
000010        2007
000010        2008
000010        2009
000010        2010
000010        2011
000010        2015
000010        2016
000012        2013
000012        2014
000012        2015
000012        2016


stata中应该如何操作呢?
二维码

扫码加我 拉你入群

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

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

关键词:Violation generated generate install example

沙发
ritaing 发表于 2021-10-21 16:26:52
试一试这个吧
  1. bys symbol (violationyear):gen allyear=violationyear[1]
  2. bys symbol (violationyear):replace allyear=allyear[_n-1]+","+violationyear if _n>1
  3. bys symbol (violationyear):replace allyear=allyear[_N]

  4. drop violationyear
  5. duplicates drop symbol allyear,force

  6. split allyear,parse(,) gen(year)
  7. drop allyear
  8. reshape long year,i(symbol) j(id)
  9. drop id
  10. drop if year==""
  11. duplicates drop symbol year,force
  12. sort symbol year
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 热心帮助其他会员

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

藤椅
yihenglu 学生认证  发表于 2021-10-21 19:43:27
ritaing 发表于 2021-10-21 16:26
试一试这个吧
非常感谢!完美解决了我的问题!

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-27 06:17