楼主: pertain
4468 9

[面板数据求助] 如何找到面板数据中第一个非缺失值 [推广有奖]

已卖:3857份资源

院士

8%

还不是VIP/贵宾

-

威望
0
论坛币
248003 个
通用积分
3495.9662
学术水平
29 点
热心指数
30 点
信用等级
19 点
经验
46596 点
帖子
1094
精华
0
在线时间
4171 小时
注册时间
2004-11-13
最后登录
2025-9-30

楼主
pertain 在职认证  发表于 2013-11-28 18:24:04 |AI写论文
888论坛币
我想找到面板数据里面,某个变量(例如:yvar)第一个非缺失值:
  1. id      year      yvar    first_non-missing_yvar
  2. ------------------------------------------------
  3. 100     1995      23      23        
  4. 100     1996      45      23      
  5. 100     1997      66      23      
  6. 100     1998      87      23        
  7. 200     1995      .       54         
  8. 200     1996      54      54           
  9. 200     1997      33      54         
  10. 200     1998      49      54
复制代码
如上,生成一个新变量:first_non-missing_yvar

请高手指教。

关键词:面板数据 缺失值 missing First SSIN 如何

沙发
hplcdadong 发表于 2013-11-28 19:01:29
Assume your original file is myfile.dta

use myfile, clear
preserve
collapse (firstnm) firstyvar=yvar, by(id)
save mytemp, replace
restore
merge m:1 id using mytemp, nogen
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
ywh19860616 + 3 + 3 + 3 很受启发

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

藤椅
bbs0805 发表于 2013-11-28 20:13:53
不错!

板凳
hplcdadong 发表于 2013-11-28 21:04:55
Another method:

ssc install egenmore, replace        //install the user-written command egenmore
egen firstyvar=first(yvar), by(id)         
replace  firstyvar=firstyvar[_n+1] if missing(firstyvar)

报纸
hplcdadong 发表于 2013-11-28 21:34:20
Just found that the second method does not work so well if "yvar" have multiple missing values for each unique "id". Sorry for this.

地板
hplcdadong 发表于 2013-11-28 21:51:58
Here is the right second method (work for any condition):

ssc install egenmore, replace        //install the user-written command egenmore
egen firstyvar=first(yvar), by(id)         
replace  firstyvar=firstyvar[_N] if missing(firstyvar)

7
pertain 在职认证  发表于 2013-11-29 00:13:44
hplcdadong 发表于 2013-11-28 21:51
Here is the right second method (work for any condition):

ssc install egenmore, replace        / ...
  1. ssc install egenmore, replace        //install the user-written command egenmore
  2. egen firstyvar=first(yvar), by(id)         
  3. replace  firstyvar=firstyvar[_N] if missing(firstyvar)
复制代码
这个是乱弹琴吧,用最后一个数值作为第一个非缺失值?

8
hplcdadong 发表于 2013-11-29 07:21:13
You are right, the above second method still does not work so well for complex conditions. Just think there should be a simpler method than the first one. Try the following in your data:

clear
input id year yvar
100 1995 23
100 1996 45
100 1997 66
200 1995 .
200 1996 .
200 1997 33
200 1998 .
200 1999 45
300 1995 .
300 1996 .
end

egen firstyvar=first(yvar), by(id)        
bysort id (firstyvar): replace firstyvar=firstyvar[_n-1] if missing(firstyvar)

9
sungmoo 发表于 2013-11-30 17:42:10
g m=mi(yvar)
bys m id (year): g f=yvar if _n==1
bys id (year): egen first=min(f)
drop m f

10
xiangyu71 发表于 2019-6-10 16:27:50
pertain 发表于 2013-11-29 00:13
这个是乱弹琴吧,用最后一个数值作为第一个非缺失值?
这个是对的,他重新生成了一个变量。

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

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