楼主: 阿袋
6076 6

[学习心得] stata skills 3:用xfill填充序列,类似excel中用鼠标下拉来填充序列 [推广有奖]

贵宾

院士

16%

还不是VIP/贵宾

-

TA的文库  其他...

各科好书新书

投资人生

论文写作投稿实战

威望
0
论坛币
568506 个
通用积分
150.1218
学术水平
304 点
热心指数
347 点
信用等级
246 点
经验
88776 点
帖子
1683
精华
5
在线时间
2895 小时
注册时间
2007-6-10
最后登录
2024-3-24

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
xfill貌似不是官方命定,所以不同通过ssc来安装,在stata命定窗口中输入
net from http://www.sealedenvelope.com/ 然后找到xfill进行安装。
真的很方便,谁用谁知道!

帖子来源:http://www.sealedenvelope.com/stata/xfill/
xfill
A Stata program to fill in values within clusters

By Tony Brady

If you haven't already done so, you may find it useful to read the article on xtabbecause it discusses what we mean by longitudinal data and static variables.

xfill is a utility that 'fills in' static variables. It replaces missing values in a cluster with the unique non-missing value within that cluster. It's easiest to see what's meant by this with an example.

Example

To follow this example in Stata type:

use http://www.sealedenvelope.com/stata/long.dta

in the Stata command window.

Look at the variable sex in our example dataset (long.dta):

sex is a static variable since it does not change within cluster (in this case the cluster is the patient). As is commonly the case with static variables, it has been recorded only in the first record within cluster. To fill in the missing values we type

. xfill sex, i(idnum)

So now our data looks like this:

Why would we want to do this? The reason is that it makes the output from other longitudinal commands easier to interpret when performed in subgroups. For instance, look at the results of xcount within by groups before we filled in sex:

This suggests that we have 14 males, 1 female and 14 patients whose gender is unknown. In fact we know the sex of all 15 patients in this dataset. The 14 phantom patients of unknown gender are being produced because patients are recorded as either male or female in the 1st record and as unknown sex in 2nd and subsequent records. One patient only has a 1st record and no subsequent records which is why the number of patients of unknown gender isn't 15.

The simple solution to this problem is to replace the missing values with their true values, in this case the patient's sex:

Now the 14 patients of apparently unknown gender are eliminated from our results. Similar problems occur when summarising static variables using if andin clauses. xfill prevents such confusion arising.




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:skills Skill Stata EXCEL kills excel

沙发
luckykong 发表于 2015-5-18 23:17:26 |只看作者 |坛友微信交流群
  1. by idnum: replace sex = sex[_n-1] if sex == .
复制代码

文中举的例子,这样也可以啊~
那么xfill还有其余的功能么?
已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
日新少年 + 1 + 1 + 1 精彩帖子
SpencerMeng + 10 感谢分享

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

使用道具

藤椅
阿袋 发表于 2015-5-18 23:32:28 |只看作者 |坛友微信交流群
luckykong 发表于 2015-5-18 23:17
文中举的例子,这样也可以啊~
那么xfill还有其余的功能么?
厉害,效果应该是一样的,我暂时只用到这个功能

使用道具

板凳
silencezou 学生认证  发表于 2015-5-19 14:51:54 |只看作者 |坛友微信交流群
看看也许以后能用

使用道具

报纸
玄一无相 在职认证  学生认证  发表于 2019-12-11 16:16:16 |只看作者 |坛友微信交流群
luckykong 发表于 2015-5-18 23:17
文中举的例子,这样也可以啊~
那么xfill还有其余的功能么?
这个写法我好像报错呢

使用道具

地板
lipeizhu 发表于 2021-4-21 11:21:49 |只看作者 |坛友微信交流群
同学,你好呀,因为毕业论文的程序需要用到xfill这个命令,你可以教我一下如何安装这个命令吗,因为我试了你在论坛上说的方法,目前发觉不行,希望同学可以帮帮忙,谢谢

使用道具

7
lipeizhu 发表于 2021-4-21 11:23:45 |只看作者 |坛友微信交流群
玄一无相 发表于 2019-12-11 16:16
这个写法我好像报错呢
同学,你好呀,因为毕业论文的程序需要用到xfill这个命令,你会安装这个命令吗,如果同学会的话,希望可以加qq沟通,1459551697@qq.com.谢谢

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-25 07:51