楼主: Lee_iris
11473 1

[学习心得] stata egen函数count和anycount [推广有奖]

已卖:1253份资源

教授

47%

还不是VIP/贵宾

-

威望
0
论坛币
1167 个
通用积分
1196.5565
学术水平
36 点
热心指数
43 点
信用等级
35 点
经验
14504 点
帖子
893
精华
0
在线时间
1517 小时
注册时间
2019-3-5
最后登录
2026-1-4

楼主
Lee_iris 在职认证  学生认证  发表于 2022-7-29 23:40:34 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

count是stata中用于计数的命令,但在egen里的用法略有不同,比较两个命令:



anycount(varlist), values(integer numlist) may not be combined with by.  It returns the number of variables in varlist for which values are equal to any integer value in a supplied numlist.  Values for any observations excluded by either if or in are set to 0 (not missing).  Also see anyvalue(varname) and anymatch(varlist).

count(exp)     (allows by varlist:)   creates a constant (within varlist) containing the number of nonmissing observations of exp.  Also see rownonmiss() and rowmiss().






二维码

扫码加我 拉你入群

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

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

关键词:anycount Count Stata egen tata

沙发
Lee_iris 在职认证  学生认证  发表于 2022-7-30 18:19:08
之前想生成一个变量,等于某一个变量中出现某个值的次数,需要分类统计,用的是

  1. bys city: egen x=count(r) if r==8
复制代码


问题是只有 r==8那行有x的值,其他的行x是缺失

  1.         sort city x
  2.         replace x=x[_n-1] if x==.       
复制代码


所以又加了两行命令,就可以了。

试了下这种情况用count(r==8)和anycount都不太对。

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

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