楼主: cast0r
4095 4

[数据管理求助] 【悬赏求解答】stata用replace分组,结果运行出错,是什么原因?内附测试文件。 [推广有奖]

  • 0关注
  • 0粉丝

已卖:1份资源

大专生

95%

还不是VIP/贵宾

-

威望
0
论坛币
1453 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
421 点
帖子
14
精华
0
在线时间
107 小时
注册时间
2010-3-3
最后登录
2022-2-28

楼主
cast0r 发表于 2015-7-21 18:58:05 |AI写论文
100论坛币
今天处理数据,发现一个诡异的问题:
有一个变量income,float格式,我想建一个新变量group,根据income的大小分成7个组

group   income   
1          (0-0.1]   小括号代表不包含,中括号代表包含。
2          (0.1-0.5]
3          (0.5-2]
4          (2-5]
5          (5-10]
6          (10-30]
7          (30+]

用如下命令处理,却发现0.1的观测值没有分到group=1里,而是被分到了group=2里,其他组都没有问题。
运行了好几遍都是如此,我用的stata14 for Mac,发现问题以后装stata13试了下,又用stata12 pc版试了下,都是这个问题,真是百思不得其解。请问有人知道原因吗?

附上数据文件、do文件和log日志,各位可以试一下。谢谢!

  1. gen group=.

  2. replace group=1 if income<=0.1  
  3. replace group=2 if income>0.1 & income<=0.5
  4. replace group=3 if income>0.5 & income<=2
  5. replace group=4 if income>2 & income<=5
  6. replace group=5 if income>5 & income<=10
  7. replace group=6 if income>10 & income<=30
  8. replace group=7 if income>30

  9. list
复制代码

程序运行日志:

. gen group=.
(102 missing values generated)

.
. replace group=1 if income<=0.1  
(0 real changes made)

. replace group=2 if income>0.1 & income<=0.5
(23 real changes made)

. replace group=3 if income>0.5 & income<=2
(31 real changes made)

. replace group=4 if income>2 & income<=5
(16 real changes made)

. replace group=5 if income>5 & income<=10
(9 real changes made)

. replace group=6 if income>10 & income<=30
(15 real changes made)

. replace group=7 if income>30
(8 real changes made)

.
. list

     +----------------+
     | income   group |
     |----------------|
  1. |     .1       2 |
  2. |     .1       2 |
  3. |     .1       2 |
  4. |     .1       2 |
  5. |     .1       2 |
     |----------------|
  6. |     .1       2 |
  7. |     .1       2 |
  8. |     .1       2 |
  9. |     .1       2 |
10. |     .1       2 |
     |----------------|
11. |     .1       2 |
12. |     .1       2 |
13. |    .15       2 |
14. |     .2       2 |
15. |     .2       2 |
     |----------------|
16. |     .2       2 |
17. |     .3       2 |
18. |     .3       2 |
19. |     .3       2 |
20. |     .4       2 |
     |----------------|
21. |     .5       2 |
22. |     .5       2 |
23. |     .5       2 |
24. |     .6       3 |
25. |     .6       3 |
     |----------------|

Stata文件.zip (2.43 KB) 本附件包括:
  • test.do
  • test.log
  • test_stata11or12_format.dta
  • test_stata14_format.dta



最佳答案

andruw 查看完整内容

code改为具体原因,可然后
关键词:replace place Stata 是什么原因 运行出错 replace income

回帖推荐

andruw 发表于2楼  查看完整内容

code改为具体原因,可然后

沙发
andruw 在职认证  发表于 2015-7-21 18:58:06
code改为
  1. gen group=.

  2. replace group=1 if income<= float(0.1)  
  3. replace group=2 if income>float(0.1) & income<=0.5
  4. replace group=3 if income>0.5 & income<=2
  5. replace group=4 if income>2 & income<=5
  6. replace group=5 if income>5 & income<=10
  7. replace group=6 if income>10 & income<=30
  8. replace group=7 if income>30

  9. list
复制代码
具体原因,可
  1. help float
复制代码
然后
  1. (See [U] 13.11 Precision and problems therein for more information.)
复制代码
已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
cast0r + 1 + 1 + 1 精彩帖子
夏目贵志 + 10 + 10 + 2 + 2 热心帮助其他会员

总评分: 经验 + 10  论坛币 + 10  学术水平 + 3  热心指数 + 3  信用等级 + 1   查看全部评分

藤椅
cast0r 发表于 2015-7-21 19:11:02
补充,用
replace group=1 if income==0.1
也无法正确分组。

板凳
cast0r 发表于 2015-7-21 23:25:27
andruw 发表于 2015-7-21 18:58
code改为具体原因,可然后
非常感谢!原来是存储格式的问题,搜索了下“float 0.1”补充了很多知识,蛮有趣的。

报纸
发发图 发表于 2022-8-25 21:25:29 来自手机
cast0r 发表于 2015-7-21 23:25
非常感谢!原来是存储格式的问题,搜索了下“float 0.1”补充了很多知识,蛮有趣的。
您好,请问一下最后是如何解决的 我也遇到了类似的问题,望赐教,非常感激!

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

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