楼主: steve66
9017 12

[其他] 请教,如何筛选数据 [推广有奖]

11
katherina_Fei 发表于 2008-8-23 19:58:00

哇,楼上提供的这个好神奇啊,太谢谢了。我用了以后,激动地跳了起来,哇,太棒了。谢谢,谢谢。

楼上的真是高手啊。能不能再帮我看看一个问题?如下,

公司名   year  data6  data19 data 21

a       1992   23     34     .

a       1993   34     35      .

a       1994   23     4       5

b       1992   23     5       .

b       1993   3      4       6

b       1994   5      2       7

当 a 公司的data21在1992年和1993年缺失时,想把1994年的数据值赋给1992年和1993年。请问怎么实现啊,谢谢大侠!!

也就是要把上面的数据改成

公司名   year  data6  data19 data 21

a       1992   23     34     5

a       1993   34     35      5

a       1994   23     4       5

b       1992   23     5       6

b       1993   3      4       6

b       1994   5      2       7

自己解答这个问题,嘻嘻。刚刚在stata的网站上找到了类似的帮助信息,

How can I replace missing values with previous or following nonmissing values or within sequences?

Without tsset: copying nonmissing values

Let us first look at the case where you have not tsset your data (see, for example, [TS] tsset for an explanation), but we will assume that the data have been put in the correct sort order, say, by typing

 . sort time 

If missing values occurred singly, then they could be replaced by the previous value

 . replace myvar = myvar[_n-1] if missing(myvar) 

or by the following value

 . replace myvar = myvar[_n+1] if missing(myvar) 

Here the subscript notation used is that _n always refers to any given observation, _n−1 to the previous observation and _n+1 to the following observation, given the current sort order. There is not, of course, any observation before the first, or after the last, so myvar[0] is always missing, as is myvar for any observation number that is negative or greater than the number of observations in the data.

[此贴子已经被作者于2008-8-25 20:48:44编辑过]

12
arlionn 在职认证  发表于 2008-8-24 09:20:00

更为简洁的处理方法:

tsset code year

xtbalance, range(1992 1994) miss(_all)

(xtbalance下载地址:https://bbs.pinggu.org/thread-226846-1-1.html)

13
award 发表于 2011-8-14 17:04:27
ccapboss 发表于 2008-7-30 15:27
用这样两个数据文件:1:就是你上面的数据:a股票代码 b股票简称 c统计日期 d行业 e行业代码 f ROE>200% ...
STATA11.0版本不能这样使用了,应该这样来
用这样两个数据文件:

1:就是你上面的数据:a股票代码 b股票简称 c统计日期 d行业 e行业代码 f ROE>200%的股票代码

2:构造一个含一个变量的数据文件,该变量名称是1的第一个变量a,取值为1中最后一个变量f的观测值

首先打开 文件1 (作为master file)
其次,命令窗口中输入
merge 1:m a using 数据文件2,回车
然后输入
drop  if _merge==2|_merge==3  回车,即可

或者输入
keep if _merge==1  回车即可

冲动是魔鬼

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

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