楼主: vivian晨
25642 16

[数据管理求助] 【附数据】STATA如何根据某一条件批量改变变量值 [推广有奖]

11
fei03 发表于 2019-1-4 10:35:28
黃河泉 发表于 2019-1-4 06:59
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并 ...
我想用上市公司的面板数据做实证,其中id是公司代码,year是年份,p是公司注册地,但有的公司注册地信息不全,需要填补。尝试了您之前说的向下和向上填充,即bysort id :replace p=p[_n-1] if p==.和bysort id :replace p=p[_n+1] if p==.以及bysort id year:replace p=p[_n+1] if p==. 都无法实现。

12
黃河泉 在职认证  发表于 2019-1-4 10:47:36
fei03 发表于 2019-1-4 10:35
我想用上市公司的面板数据做实证,其中id是公司代码,year是年份,p是公司注册地,但有的公司注册地信息不 ...
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并见说明),将原始 Stata 资料中具有”代表性”的一部分资料列出,以供有意回答者实验之用,并能提供具体操作指令。
2.        并请参考 http://www.jianshu.com/p/9870080fe769,  https://bbs.pinggu.org/thread-5048204-1-1.html, 与 https://bbs.pinggu.org/thread-5917273-1-1.html

13
fei03 发表于 2019-1-4 11:39:48
黃河泉 发表于 2019-1-4 10:47
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并 ...
[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year long p
2279 2009  .
2279 2010  4
2279 2011  .
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009  .
2280 2010  .
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
2281 2009 21
end
第一列是公司代码id,第二列是年份year,第三列是公司注册地信息。想要达到这样的填充效果:

2279 2009  4
2279 2010  4
2279 2011  4
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009 19
2280 2010 19
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
2281 2009 21
之前尝试了bysort id :replace p=p[_n-1] if p==.和bysort id :replace p=p[_n+1] if p==.以及bysort id year:replace p=p[_n+1] if p==.都无法实现。



14
fei03 发表于 2019-1-4 11:43:08
黃河泉 发表于 2019-1-4 10:47
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并 ...
[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year long p
2279 2009  .
2279 2010  4
2279 2011  .
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009  .
2280 2010  .
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
end

其中,id是公司代码,year是年份,p是公司注册地。想要达到这样的结果

2279 2009  4
2279 2010  4
2279 2011  4
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009 19
2280 2010 19
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19

尝试过bysort id :replace p=p[_n-1] if p==.和bysort id :replace p=p[_n+1] if p==.以及bysort id year:replace p=p[_n+1] if p==. 都无法实现。



15
fei03 发表于 2019-1-4 12:12:56

RE: 【附数据】STATA如何根据某一条件批量改变变量值

黃河泉 发表于 2019-1-4 10:47
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并 ...
[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year long p
2279 2009  .
2279 2010  4
2279 2011  .
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009  .
2280 2010  .
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
end
其中id 是公司代码,year是年份,p是公司注册地(不随时间改变),想要达到如下填充结果。

2279 2009  4
2279 2010  4
2279 2011  4
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009 19
2280 2010 19
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
尝试过bysort id :replace p=p[_n-1] if p==.和bysort id :replace p=p[_n+1] if p==.以及bysort id year:replace p=p[_n+1] if p==. 都无法实现。

16
fei03 发表于 2019-1-4 13:18:03
黃河泉 发表于 2019-1-4 06:59
你若要问程序,永远附上相关资料;若附上资料,永远用 dataex 印出资料。
1.        先 ssc install dataex (并 ...
[CODE]
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int year long p
2279 2009  .
2279 2010  4
2279 2011  .
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009  .
2280 2010  .
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
2281 2009 21
end
其中id是公司代码,year是年份, p是公司注册地(不随时间改变),想要达到如下填充结果

input long id int year long p
2279 2009  4
2279 2010  4
2279 2011  4
2279 2012  4
2279 2013  4
2279 2014  4
2279 2015  4
2279 2016  4
2280 2009 19
2280 2010 19
2280 2011 19
2280 2012 19
2280 2013 19
2280 2014 19
2280 2015 19
2280 2016 19
2281 2009 21
end
尝试过bysort id :replace p=p[_n-1] if p==.和bysort id :replace p=p[_n+1] if p==.以及bysort id year:replace p=p[_n+1] if p==. 都无法实现。

17
Uhlancat 学生认证  发表于 2021-3-16 10:22:56

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

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