楼主: danna-33
6251 11

[数据管理求助] 数据细分成各组后,怎么在各组中取一个变量前十位的样本组?stata命令如何实现? [推广有奖]

  • 0关注
  • 0粉丝

本科生

27%

还不是VIP/贵宾

-

威望
0
论坛币
7 个
通用积分
1.0008
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
1267 点
帖子
54
精华
0
在线时间
48 小时
注册时间
2016-6-7
最后登录
2023-9-20

楼主
danna-33 发表于 2017-8-29 14:35:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
如题
我写的命令是:bys element item t : list y element item area t in 1/10,table noobs
我希望得到的是变量y 经过不同的分组处理(依次按不同的element item t),能把由高到低的y值取出来,并且能以表格形式呈现
stata 报错说 'in' may not be combined with 'by'
请问哪位大神能指点指点我吗?到底应该怎么写命令啊?
二维码

扫码加我 拉你入群

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

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

关键词:stata命令 Stata tata 如何实现 combined

回帖推荐

黃河泉 发表于10楼  查看完整内容

试试其它你应该可以自己做!

沙发
deem 学生认证  发表于 2017-8-29 14:54:33
  1. bys element item t : keep if _n >= 10
复制代码

藤椅
danna-33 发表于 2017-8-29 15:08:16
deem 发表于 2017-8-29 14:54
谢谢,但是我想保留的是变量y 在每个组中从大到小排列后,取前十名这样子,也能够做到吗?

板凳
黃河泉 在职认证  发表于 2017-8-29 17:22:03
尔后建議用 dataex (先 ssc install dataex 并见说明) 将原始 Stata 资料中具有”代表性”的一部分资料列出,以供有意回答者实验之用,并能提供具体操作指令。并请参考https://bbs.pinggu.org/thread-5048204-1-1.htmlhttps://bbs.pinggu.org/thread-5917273-1-1.html

报纸
danna-33 发表于 2017-8-29 21:03:04
黃河泉 发表于 2017-8-29 17:22
尔后建議用 dataex (先 ssc install dataex 并见说明) 将原始 Stata 资料中具有”代表性”的一部分资料列出 ...
谢谢黄老师,我会学学看

地板
danna-33 发表于 2017-8-29 21:43:16
黃河泉 发表于 2017-8-29 17:22
尔后建議用 dataex (先 ssc install dataex 并见说明) 将原始 Stata 资料中具有”代表性”的一部分资料列出 ...
----------------------- copy starting from the next line -----------------------
  1. * Example generated by -dataex-. To install: ssc install dataex
  2. clear
  3. input int t str44(area item) str14 element double y
  4. 2000 "Afghanistan" "Cotton lint" "Production"       18500
  5. 2001 "Afghanistan" "Cotton lint" "Production"       18500
  6. 2002 "Afghanistan" "Cotton lint" "Production"       18500
  7. 2003 "Afghanistan" "Cotton lint" "Production"       10890
  8. 2004 "Afghanistan" "Cotton lint" "Production"        9240
  9. 2005 "Afghanistan" "Cotton lint" "Production"       10890
  10. 2006 "Afghanistan" "Cotton lint" "Production"       10560
  11. 2007 "Afghanistan" "Cotton lint" "Production"       11567
  12. 2008 "Afghanistan" "Cotton lint" "Production"       11550
  13. 2009 "Afghanistan" "Cotton lint" "Production"       14148
  14. 2010 "Afghanistan" "Cotton lint" "Production"       10890
  15. 2011 "Afghanistan" "Cotton lint" "Production"       10890
  16. 2012 "Afghanistan" "Cotton lint" "Production"       11979
  17. 2013 "Afghanistan" "Cotton lint" "Production"       13917
  18. 2014 "Afghanistan" "Cotton lint" "Production"       14000
  19. 2000 "Afghanistan" "Maize"       "Area harvested"   96000
  20. 2001 "Afghanistan" "Maize"       "Area harvested"   80000
  21. 2002 "Afghanistan" "Maize"       "Area harvested"  100000
  22. 2003 "Afghanistan" "Maize"       "Area harvested"  250000
  23. 2004 "Afghanistan" "Maize"       "Area harvested"  250000
  24. 2005 "Afghanistan" "Maize"       "Area harvested"  261000
  25. 2006 "Afghanistan" "Maize"       "Area harvested"  137000
  26. 2007 "Afghanistan" "Maize"       "Area harvested"  137000
  27. 2008 "Afghanistan" "Maize"       "Area harvested"  137000
  28. 2009 "Afghanistan" "Maize"       "Area harvested"  140000
  29. 2010 "Afghanistan" "Maize"       "Area harvested"  183000
  30. 2011 "Afghanistan" "Maize"       "Area harvested"  183000
  31. 2012 "Afghanistan" "Maize"       "Area harvested"  141000
  32. 2013 "Afghanistan" "Maize"       "Area harvested"  142000
  33. 2014 "Afghanistan" "Maize"       "Area harvested"  127000
  34. 2000 "Afghanistan" "Maize"       "Production"      115000
  35. 2001 "Afghanistan" "Maize"       "Production"      160000
  36. 2002 "Afghanistan" "Maize"       "Production"      298000
  37. 2003 "Afghanistan" "Maize"       "Production"      210000
  38. 2004 "Afghanistan" "Maize"       "Production"      400000
  39. 2005 "Afghanistan" "Maize"       "Production"      315000
  40. 2006 "Afghanistan" "Maize"       "Production"      359000
  41. 2007 "Afghanistan" "Maize"       "Production"      360000
  42. 2008 "Afghanistan" "Maize"       "Production"      360000
  43. 2009 "Afghanistan" "Maize"       "Production"      300000
  44. 2010 "Afghanistan" "Maize"       "Production"      301000
  45. 2011 "Afghanistan" "Maize"       "Production"      300120
  46. 2012 "Afghanistan" "Maize"       "Production"      310000
  47. 2013 "Afghanistan" "Maize"       "Production"      312000
  48. 2014 "Afghanistan" "Maize"       "Production"      316000
  49. 2000 "Afghanistan" "Rice, paddy" "Area harvested"  130000
  50. 2001 "Afghanistan" "Rice, paddy" "Area harvested"  121000
  51. 2002 "Afghanistan" "Rice, paddy" "Area harvested"  135000
  52. 2003 "Afghanistan" "Rice, paddy" "Area harvested"  145000
  53. 2004 "Afghanistan" "Rice, paddy" "Area harvested"  195000
  54. 2005 "Afghanistan" "Rice, paddy" "Area harvested"  160000
  55. 2006 "Afghanistan" "Rice, paddy" "Area harvested"  160000
  56. 2007 "Afghanistan" "Rice, paddy" "Area harvested"  170000
  57. 2008 "Afghanistan" "Rice, paddy" "Area harvested"  190000
  58. 2009 "Afghanistan" "Rice, paddy" "Area harvested"  200000
  59. 2010 "Afghanistan" "Rice, paddy" "Area harvested"  208000
  60. 2011 "Afghanistan" "Rice, paddy" "Area harvested"  210000
  61. 2012 "Afghanistan" "Rice, paddy" "Area harvested"  205000
  62. 2013 "Afghanistan" "Rice, paddy" "Area harvested"  205000
  63. 2014 "Afghanistan" "Rice, paddy" "Area harvested"  220000
  64. 2000 "Afghanistan" "Rice, paddy" "Production"      260000
  65. 2001 "Afghanistan" "Rice, paddy" "Production"      242000
  66. 2002 "Afghanistan" "Rice, paddy" "Production"      388000
  67. 2003 "Afghanistan" "Rice, paddy" "Production"      434000
  68. 2004 "Afghanistan" "Rice, paddy" "Production"      463000
  69. 2005 "Afghanistan" "Rice, paddy" "Production"      485000
  70. 2006 "Afghanistan" "Rice, paddy" "Production"      540000
  71. 2007 "Afghanistan" "Rice, paddy" "Production"      552000
  72. 2008 "Afghanistan" "Rice, paddy" "Production"      612000
  73. 2009 "Afghanistan" "Rice, paddy" "Production"      645000
  74. 2010 "Afghanistan" "Rice, paddy" "Production"      672000
  75. 2011 "Afghanistan" "Rice, paddy" "Production"      672000
  76. 2012 "Afghanistan" "Rice, paddy" "Production"      500000
  77. 2013 "Afghanistan" "Rice, paddy" "Production"      512094
  78. 2014 "Afghanistan" "Rice, paddy" "Production"      537000
  79. 2000 "Afghanistan" "Wheat"       "Area harvested" 2029000
  80. 2001 "Afghanistan" "Wheat"       "Area harvested" 1779000
  81. 2002 "Afghanistan" "Wheat"       "Area harvested" 1742000
  82. 2003 "Afghanistan" "Wheat"       "Area harvested" 2320000
  83. 2004 "Afghanistan" "Wheat"       "Area harvested" 1888000
  84. 2005 "Afghanistan" "Wheat"       "Area harvested" 2342000
  85. 2006 "Afghanistan" "Wheat"       "Area harvested" 2444000
  86. 2007 "Afghanistan" "Wheat"       "Area harvested" 2466000
  87. 2008 "Afghanistan" "Wheat"       "Area harvested" 2139000
  88. 2009 "Afghanistan" "Wheat"       "Area harvested" 2575000
  89. 2010 "Afghanistan" "Wheat"       "Area harvested" 2354000
  90. 2011 "Afghanistan" "Wheat"       "Area harvested" 2232000
  91. 2012 "Afghanistan" "Wheat"       "Area harvested" 2512000
  92. 2013 "Afghanistan" "Wheat"       "Area harvested" 2552922
  93. 2014 "Afghanistan" "Wheat"       "Area harvested" 2653746
  94. 2000 "Afghanistan" "Wheat"       "Production"     1469000
  95. 2001 "Afghanistan" "Wheat"       "Production"     1597000
  96. 2002 "Afghanistan" "Wheat"       "Production"     2686000
  97. 2003 "Afghanistan" "Wheat"       "Production"     3480000
  98. 2004 "Afghanistan" "Wheat"       "Production"     2390000
  99. 2005 "Afghanistan" "Wheat"       "Production"     4266000
  100. 2006 "Afghanistan" "Wheat"       "Production"     3363000
  101. 2007 "Afghanistan" "Wheat"       "Production"     4484000
  102. 2008 "Afghanistan" "Wheat"       "Production"     2623000
  103. 2009 "Afghanistan" "Wheat"       "Production"     5064000
  104. end
复制代码
------------------ copy up to and including the previous line ------------------
黄老师,已经截取了代表性的数据
我想得到的是每种农产品(玉米、大豆等)耕种面积(或产量)在某年(2004或2005)世界前十的国家排名(及其相应的耕种面积或产量),并制成一个表格,我自己写了一句命令
bys element item t : list y element item area t in 10,table sepby() noobs
但是stata报错,不知道您有没有好办法,谢谢

7
danna-33 发表于 2017-8-29 21:46:19
黃河泉 发表于 2017-8-29 17:22
尔后建議用 dataex (先 ssc install dataex 并见说明) 将原始 Stata 资料中具有”代表性”的一部分资料列出 ...
  1. bys element item t : list y element item area t in 10,table sepby() noobs
复制代码

8
黃河泉 在职认证  发表于 2017-8-30 08:08:20
danna-33 发表于 2017-8-29 21:46
1. 你的资料不具代表性,只有一个国家无法排序!2. element 变量有何角色?还是根本就不需要!3. 请试试
  1. dataex t area item element y if year == 2010 & item == "Maize"
复制代码

9
danna-33 发表于 2017-8-30 08:45:30
黃河泉 发表于 2017-8-30 08:08
1. 你的资料不具代表性,只有一个国家无法排序!2. element 变量有何角色?还是根本就不需要!3. 请试试
  1. * Example generated by -dataex-. To install: ssc install dataex
  2. clear
  3. input int t str44(area item) str14 element double y
  4. 2010 "Afghanistan"                           "Maize" "Area harvested"    183000
  5. 2010 "Afghanistan"                           "Maize" "Production"        301000
  6. 2010 "Albania"                               "Maize" "Area harvested"     54200
  7. 2010 "Albania"                               "Maize" "Production"        362000
  8. 2010 "Algeria"                               "Maize" "Area harvested"       139
  9. 2010 "Algeria"                               "Maize" "Production"           359
  10. 2010 "Angola"                                "Maize" "Area harvested"   1489815
  11. 2010 "Angola"                                "Maize" "Production"       1072737
  12. 2010 "Antigua and Barbuda"                   "Maize" "Area harvested"        40
  13. 2010 "Antigua and Barbuda"                   "Maize" "Production"            63
  14. 2010 "Argentina"                             "Maize" "Area harvested"   2904035
  15. 2010 "Argentina"                             "Maize" "Production"      22663095
  16. 2010 "Armenia"                               "Maize" "Area harvested"      2729
  17. 2010 "Armenia"                               "Maize" "Production"         12751
  18. 2010 "Australia"                             "Maize" "Area harvested"     59000
  19. 2010 "Australia"                             "Maize" "Production"        328000
  20. 2010 "Austria"                               "Maize" "Area harvested"    201100
  21. 2010 "Austria"                               "Maize" "Production"       2168837
  22. 2010 "Azerbaijan"                            "Maize" "Area harvested"     29870
  23. 2010 "Azerbaijan"                            "Maize" "Production"        136061
  24. 2010 "Bahamas"                               "Maize" "Area harvested"        90
  25. 2010 "Bahamas"                               "Maize" "Production"           678
  26. 2010 "Bangladesh"                            "Maize" "Area harvested"    152009
  27. 2010 "Bangladesh"                            "Maize" "Production"        887391
  28. 2010 "Barbados"                              "Maize" "Area harvested"        90
  29. 2010 "Barbados"                              "Maize" "Production"           259
  30. 2010 "Belarus"                               "Maize" "Area harvested"    111668
  31. 2010 "Belarus"                               "Maize" "Production"        550586
  32. 2010 "Belgium"                               "Maize" "Area harvested"     62531
  33. 2010 "Belgium"                               "Maize" "Production"        745891
  34. 2010 "Belize"                                "Maize" "Area harvested"     19862
  35. 2010 "Belize"                                "Maize" "Production"         58048
  36. 2010 "Benin"                                 "Maize" "Area harvested"    918236
  37. 2010 "Benin"                                 "Maize" "Production"       1012630
  38. 2010 "Bhutan"                                "Maize" "Area harvested"     24879
  39. 2010 "Bhutan"                                "Maize" "Production"         57663
  40. 2010 "Bolivia (Plurinational State of)"      "Maize" "Area harvested"    365354
  41. 2010 "Bolivia (Plurinational State of)"      "Maize" "Production"       1018988
  42. 2010 "Bosnia and Herzegovina"                "Maize" "Area harvested"    188752
  43. 2010 "Bosnia and Herzegovina"                "Maize" "Production"        853376
  44. 2010 "Botswana"                              "Maize" "Area harvested"     65388
  45. 2010 "Botswana"                              "Maize" "Production"         10572
  46. 2010 "Brazil"                                "Maize" "Area harvested"  12678875
  47. 2010 "Brazil"                                "Maize" "Production"      55364271
  48. 2010 "Bulgaria"                              "Maize" "Area harvested"    327500
  49. 2010 "Bulgaria"                              "Maize" "Production"       2047400
  50. 2010 "Burkina Faso"                          "Maize" "Area harvested"    790321
  51. 2010 "Burkina Faso"                          "Maize" "Production"       1133452
  52. 2010 "Burundi"                               "Maize" "Area harvested"    119432
  53. 2010 "Burundi"                               "Maize" "Production"        126412
  54. 2010 "Cabo Verde"                            "Maize" "Area harvested"     32028
  55. 2010 "Cabo Verde"                            "Maize" "Production"          7047
  56. 2010 "Cambodia"                              "Maize" "Area harvested"    214000
  57. 2010 "Cambodia"                              "Maize" "Production"        773269
  58. 2010 "Cameroon"                              "Maize" "Area harvested"    846130
  59. 2010 "Cameroon"                              "Maize" "Production"       1670321
  60. 2010 "Canada"                                "Maize" "Area harvested"   1202900
  61. 2010 "Canada"                                "Maize" "Production"      11714500
  62. 2010 "Central African Republic"              "Maize" "Area harvested"     92891
  63. 2010 "Central African Republic"              "Maize" "Production"        150000
  64. 2010 "Chad"                                  "Maize" "Area harvested"    263996
  65. 2010 "Chad"                                  "Maize" "Production"        215797
  66. 2010 "Chile"                                 "Maize" "Area harvested"    122547
  67. 2010 "Chile"                                 "Maize" "Production"       1357921
  68. 2010 "China"                                 "Maize" "Area harvested"  32517868
  69. 2010 "China"                                 "Maize" "Production"     177540788
  70. 2010 "China, mainland"                       "Maize" "Area harvested"  32500000
  71. 2010 "China, mainland"                       "Maize" "Production"     177425000
  72. 2010 "China, Taiwan Province of"             "Maize" "Area harvested"     17868
  73. 2010 "China, Taiwan Province of"             "Maize" "Production"        115788
  74. 2010 "Colombia"                              "Maize" "Area harvested"    522237
  75. 2010 "Colombia"                              "Maize" "Production"       1421868
  76. 2010 "Comoros"                               "Maize" "Area harvested"      2500
  77. 2010 "Comoros"                               "Maize" "Production"          5290
  78. 2010 "Congo"                                 "Maize" "Area harvested"     12851
  79. 2010 "Congo"                                 "Maize" "Production"         10500
  80. 2010 "Costa Rica"                            "Maize" "Area harvested"      9598
  81. 2010 "Costa Rica"                            "Maize" "Production"         18765
  82. 2010 "C魌e d'Ivoire"                         "Maize" "Area harvested"    324045
  83. 2010 "C魌e d'Ivoire"                         "Maize" "Production"        641610
  84. 2010 "Croatia"                               "Maize" "Area harvested"    296768
  85. 2010 "Croatia"                               "Maize" "Production"       2067815
  86. 2010 "Cuba"                                  "Maize" "Area harvested"    225608
  87. 2010 "Cuba"                                  "Maize" "Production"        324463
  88. 2010 "Czechia"                               "Maize" "Area harvested"    103300
  89. 2010 "Czechia"                               "Maize" "Production"        692600
  90. 2010 "Democratic People's Republic of Korea" "Maize" "Area harvested"    503000
  91. 2010 "Democratic People's Republic of Korea" "Maize" "Production"       1683000
  92. 2010 "Democratic Republic of the Congo"      "Maize" "Area harvested"   1484775
  93. 2010 "Democratic Republic of the Congo"      "Maize" "Production"       1155964
  94. 2010 "Denmark"                               "Maize" "Area harvested"      9500
  95. 2010 "Denmark"                               "Maize" "Production"         45700
  96. 2010 "Djibouti"                              "Maize" "Area harvested"         8
  97. 2010 "Djibouti"                              "Maize" "Production"            14
  98. 2010 "Dominica"                              "Maize" "Area harvested"       120
  99. 2010 "Dominica"                              "Maize" "Production"           170
  100. 2010 "Dominican Republic"                    "Maize" "Area harvested"     22241
  101. 2010 "Dominican Republic"                    "Maize" "Production"         36658
  102. 2010 "Ecuador"                               "Maize" "Area harvested"    440346
  103. 2010 "Ecuador"                               "Maize" "Production"        984096
  104. end
复制代码
以上是老师给的代码得到的结果,element变量只有production和areaharvested 两个内容,需要做区分以得到生产量和耕种面积两方面的前十名国家排序
谢谢老师

10
黃河泉 在职认证  发表于 2017-8-30 09:04:03
danna-33 发表于 2017-8-30 08:45
以上是老师给的代码得到的结果,element变量只有production和areaharvested 两个内容,需要做区分以得到生 ...
试试
  1. bys t element item (y): egen rank = rank(-y)
  2. keep if rank <= 10
  3. sort t element item rank y
复制代码
其它你应该可以自己做!

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

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