楼主: 天斯吾下
1650 7

[编程问题求助] if-command-versus-if-qualifier 不能连用 [推广有奖]

  • 7关注
  • 4粉丝

已卖:11份资源

副教授

53%

还不是VIP/贵宾

-

威望
0
论坛币
1176 个
通用积分
87.5911
学术水平
8 点
热心指数
14 点
信用等级
7 点
经验
92 点
帖子
443
精华
0
在线时间
713 小时
注册时间
2013-3-6
最后登录
2021-7-19

楼主
天斯吾下 学生认证  发表于 2015-9-21 23:13:08 |AI写论文
99论坛币
  1. if numofCompany==33 & LossIndicator==1 & year<2014 {
  2. count if numofCompany==33 & LossIndicator==1 & year<2014
  3. local c=r(N)
  4. dis `c'
  5. }
复制代码
由于编程需要,我必须使用以上格式,但是没有结果
  1. count if numofCompany==33 & LossIndicator==1 & year<2014
  2. local c=r(N)
  3. dis `c'
复制代码
直接使用这个命令却能得到正确结果,我谷歌了很久依然没有搞懂

以下是我搜索到的相关参考链接
http://www.statalist.org/forums/ ... ogramming-languages

http://www.stata.com/support/faq ... ersus-if-qualifier/

example.zip
下载链接: https://bbs.pinggu.org/a-1880169.html

548 Bytes

示例数据

本附件包括:

  • example.dta

最佳答案

夏目贵志 查看完整内容

if numofCompany==33 & LossIndicator==1 & year
关键词:Command versus vers Comm COM

回帖推荐

夏目贵志 发表于5楼  查看完整内容

sort numo year gen counter = 1 gen Loss = . by numo: gen b = sum(counter) if LossIndicator!=. by numo: gen c = sum(LossInd) replace Loss = c/b

沙发
夏目贵志 发表于 2015-9-21 23:13:09
if numofCompany==33 & LossIndicator==1 & year<2014 不是标准的expression。numofCompany等变量名实际取值是第一个observation的值。
你看这个例子就明白了
sysuse auto
di price
list price in 1/5
sort price
di price
list price in 1/5
所以一般不建议在generate和replace之类的命令之外使用variable name指代variable的value。应该先把想要的value取出,存在macro里,然后再用在if里。
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
dxystata + 50 + 20 + 1 + 1 + 1 精彩帖子

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

藤椅
天斯吾下 学生认证  发表于 2015-9-25 19:10:00
非常感谢,您完美的解决了我的问题!

QQ图片20150925190408.png (1.61 KB)

请无视,不知怎么删除

请无视,不知怎么删除

example1.zip

710 Bytes

请无视,不知怎么删除

本附件包括:

  • example1.dta

板凳
天斯吾下 学生认证  发表于 2015-9-29 15:34:02
夏目贵志 发表于 2015-9-21 23:13
if numofCompany==33 & LossIndicator==1 & year
再请教您一个问题,我代码确实是写好了,但是运行效率极其低下。我电脑配置应该挺高的,CPU i74930xm 4核 八线程,32GB内存。硬件上应该没有问题,在运行以下代码时CPU占用率也很高,CPU每个核心都用上了,stata占用CPU90%以上。
  1. egen numofCompany=group(Companyname)
  2. gen     LossIndicator=1 if PLforperiodNetincome<0
  3. replace LossIndicator=0 if (PLforperiodNetincome>0 | PLforperiodNetincome==0 ) & PLforperiodNetincome!=.
  4. gen Loss=.

  5. sum numofCompany
  6. local a=r(max)
  7. forvalues i=1/`a'{
  8. forvalues j=2004/2014{
  9. local d=`j'+1
  10. qui count if numofCompany==`i' & LossIndicator!=. & year<`d'
  11. local b=r(N)
  12. dis `b'
  13. qui count if numofCompany==`i' & LossIndicator==1 & year<`d'
  14. local c=r(N)

  15. if `b'!=0{
  16. replace Loss=`c'/`b' if numofCompany==`i' & LossIndicator!=. & year==`j'
  17. }
  18. }
  19. }
复制代码
我这段代码的目的就是生成Loss这个变量。Loss is Cumulative percentage of sample years that the firm reported a loss.
生成巴基斯坦这个国家的Loss变量并不难,因为公司不多,我现在在生成中国的Loss变量,几天几夜了还没完,请问有没有修改代码的办法?

Pakistan.zip

1.04 MB

stata13 巴基斯坦财务数据

本附件包括:

  • Pakistan.dta

Financial Reporting Quality of U.S. Private and Public Firms(1).pdf

870.89 KB

Loss 变量 就和这篇文章里用的一样

报纸
夏目贵志 发表于 2015-9-30 21:44:23
天斯吾下 发表于 2015-9-29 15:34
再请教您一个问题,我代码确实是写好了,但是运行效率极其低下。我电脑配置应该挺高的,CPU i74930xm 4核 ...
sort numo year
gen counter = 1
gen Loss = .
by numo: gen b = sum(counter) if LossIndicator!=.
by numo: gen c = sum(LossInd)
replace Loss = c/b

地板
夏目贵志 发表于 2015-9-30 21:44:55
另外,字符型的变量尽量不要保留在数据里。处理起来慢。

7
天斯吾下 学生认证  发表于 2015-10-2 21:24:29
夏目贵志 发表于 2015-9-30 21:44
sort numo year
gen counter = 1
gen Loss = .
非常感谢您的回答,完美地解决了我的问题,另祝您国庆快乐!

8
夏目贵志 发表于 2015-10-3 02:00:03
天斯吾下 发表于 2015-10-2 21:24
非常感谢您的回答,完美地解决了我的问题,另祝您国庆快乐!
国庆快乐~

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

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