楼主: Henryzhu
10289 5

使用bysort或者其他命令如何生成如下形式的变量number [推广有奖]

  • 6关注
  • 61粉丝

已卖:314份资源

教授

89%

还不是VIP/贵宾

-

威望
0
论坛币
11959 个
通用积分
0.2415
学术水平
45 点
热心指数
76 点
信用等级
24 点
经验
86502 点
帖子
1419
精华
0
在线时间
1026 小时
注册时间
2010-11-23
最后登录
2022-1-9

楼主
Henryzhu 在职认证  发表于 2012-11-22 22:30:13 |AI写论文
20论坛币
  • 我的原始数据如下:
    clear
    input id year    month    day
    1 2002    1    11
    1 2002    1    18
    1 2002    1    25
    1 2002    1    30
    1 2002    1    31
    1 2002    2    1
    1 2002    2    4
    1 2002    2    5
    1 2002    2    6
    1 2002    4    7
    1 2002    4    8
    1 2002    4    25
    1 2002    4    26
    end
    drop id
    gen number=.
    replace number=2 in 1
    replace number=5 in 6
    replace number=9 in 10

    根据上述变量,我希望达到的number为如下形式:

    clear
    input year        month        day        number
    2002        1        11        2
    2002        1        18        2
    2002        1        25        2
    2002        1        30        2
    2002        1        31        2
    2002        2        1        5
    2002        2        4        5
    2002        2        5        5
    2002        2        6        5
    2002        4        7        9
    2002        4        8        9
    2002        4        25        9
    2002        4        26        9
    end

非常感谢大家!



最佳答案

h3327156 查看完整内容

clear inp id year month day 1 2002 1 11 1 2002 1 18 1 2002 1 25 1 2002 1 30 1 2002 1 31 1 2002 2 1 1 2002 2 4 1 2002 2 5 1 2002 2 6 1 2002 4 7 1 2002 4 8 1 2002 4 25 1 2002 4 26 end drop id recode month (1 = 2) (2 = 5) (4=9), gen(number) 参考看看
关键词:bysort Number NUM Ber SOR replace number 如何

沙发
h3327156 发表于 2012-11-22 22:30:14
clear
inp id year    month    day
1 2002    1    11
1 2002    1    18
1 2002    1    25
1 2002    1    30
1 2002    1    31
1 2002    2    1
1 2002    2    4
1 2002    2    5
1 2002    2    6
1 2002    4    7
1 2002    4    8
1 2002    4    25
1 2002    4    26
end
drop id
recode month (1 = 2) (2 = 5) (4=9), gen(number)

参考看看
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Henryzhu + 5 + 5 + 5 热心帮助其他会员

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

藤椅
fgleric 发表于 2012-11-23 02:33:16
number的产生跟month之间的关系是?

板凳
Henryzhu 在职认证  发表于 2012-11-23 17:50:17
h3327156 发表于 2012-11-22 22:58
clear
inp id year    month    day
1 2002    1    11
非常感谢!
好像还不是我所希望的目标,之前我的数据有点问题,
我的原始数据是
clear
inp id year    month    day
1 2002    1    11
1 2002    1    18
1 2002    1    25
1 2002    1    30
1 2002    1    31
1 2002    2    1
1 2002    2    4
1 2002    2    5
1 2002    2    6
1 2002    4    7
1 2002    4    8
1 2002    4    25
1 2002    4    26
end
drop id
gen number=.
replace number=2 in 1
replace number=5 in 5
replace number=9 in 7
replace number=12 in 9
replace number=15 in 11

我希望得到的数据为
clear
input year        month        day        number
2002        1        11        2
2002        1        18        2
2002        1        25        2
2002        1        30        2
2002        1        31        5
2002        2        1        5
2002        2        4        9
2002        2        5        9
2002        2        6        12
2002        4        7        12
2002        4        8        15
2002        4        25        15
2002        4        26        15
end

好像还是有点困难,这个问题看似简单,但是貌似好像有点难度的说。

谢谢了
很高兴能来这个论坛

报纸
h3327156 发表于 2012-11-23 20:56:47
Henryzhu 发表于 2012-11-23 17:50
非常感谢!
好像还不是我所希望的目标,之前我的数据有点问题,
我的原始数据是
如果您的数剧改成那样,那么fgleric问的,也是我想知道的,
楼主您并没有交待清楚您number是想怎么产生,
这样大家是无法去回答问题的。

地板
Henryzhu 在职认证  发表于 2012-11-24 11:21:18
h3327156 发表于 2012-11-23 20:56
如果您的数剧改成那样,那么fgleric问的,也是我想知道的,
楼主您并没有交待清楚您number是想怎么产生, ...
前辈,您好!
非常抱歉我的表达能力。
我的number是想按照时间先后顺序,
缺失的值自动跟前面出现的值一样,直到没有出现缺失。
比方说最开始2002年1月11日的number为2,
2002年1月18日到2002年1月30日的number为缺失的,
则这几天的number取值为2。
2002年1月31日的number为5,
2002年2月1日的nunber缺失,
然后将2002年2月1日的nunber取值为5。
再比如2002年4月8日的number为15,
之后的number缺失,则这2002年4月25日和2002年4月26日的number也取值为15。

非常感谢前辈的建议!
很高兴能来这个论坛

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

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