楼主: weekendxzr
8363 5

[其他] 怎么让Stata循环执行do命令? [推广有奖]

  • 0关注
  • 2粉丝

已卖:175份资源

硕士生

76%

还不是VIP/贵宾

-

威望
0
论坛币
69026 个
通用积分
3.7392
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
602 点
帖子
22
精华
0
在线时间
352 小时
注册时间
2007-10-15
最后登录
2025-9-16

楼主
weekendxzr 在职认证  发表于 2012-4-19 05:43:01 |AI写论文
70论坛币
小弟现在有一个回归的命令,对三位数代码的行业进行回归然后保存到Excel。这个过程需要重复171次,每次换一个不同的行业。下面的命令中红字的部分就是表示行业代码
行业已经有了一组标准代码,从131一直到469,但是不连续。

怎样让下面这个程序自动执行171次,每次换一个行业(也就是红字的部分变换一下)呢?

clear all
use "C:\Users\Administrator\Desktop\新建文件夹\新数据2.dta", clear
keep if industry==191
xtbalance, range(2005 2007)
reg Y2 X1 X2, noc robust
est store R1
outreg2 using 2digit2.xls, cttop(OLS:C191) e(all) append
outreg2 using 2digitnoa2.xls, cttop(OLS:C191) noas append
xtabond2 Y2 X1 X2, gmm(X1) iv(zk allempl_arvenum X2) noc robust
est store G1
outreg2 using 2digit2.xls, cttop(GMM:C191) e(all) append
outreg2 using 2digitnoa2.xls, cttop(GMM:C191) noas append

最佳答案

sungmoo 查看完整内容

clear all set more off cap log close cd "C:\Users\Administrator\Desktop\新建文件夹" use "新新2位数行业新数据.dta", clear egen g=group(businessnature4) su g loc n=r(max) forv i=1/`n'{ preserve keep if g==`i' xtbalance, range(2005 2007) reg Y2 X1 X2, noc robust est store R1 outreg2 using 2digit3.xls, cttop(OLS:C`i') e(all) append outreg2 using 2digitnoa3.xls, cttop(OLS:C`i') noas app ...
关键词:Stata tata xtbalance Industry balance industry 新建文件夹 Desktop store Excel

回帖推荐

sungmoo 发表于6楼  查看完整内容

clear all set more off cap log close cd "C:\Users\Administrator\Desktop\新建文件夹" use "新新2位数行业新数据.dta", clear egen g=group(businessnature4) su g loc n=r(max) forv i=1/`n'{ preserve keep if g==`i' xtbalance, range(2005 2007) reg Y2 X1 X2, noc robust est store R1 outreg2 using 2digit3.xls, cttop(OLS:C`i') e(all) append outreg2 using 2digitnoa3.xls, cttop(OLS:C`i') noas app ...

沙发
sungmoo 发表于 2012-4-19 05:43:02
clear all
set more off
cap log close
cd "C:\Users\Administrator\Desktop\新建文件夹"
use "新新2位数行业新数据.dta", clear
egen g=group(businessnature4)
su g
loc n=r(max)
forv i=1/`n'{
preserve
keep if g==`i'
xtbalance, range(2005 2007)
reg Y2 X1 X2, noc robust
est store R1
outreg2 using 2digit3.xls, cttop(OLS:C`i') e(all) append
outreg2 using 2digitnoa3.xls, cttop(OLS:C`i') noas append
xtabond2 Y2 X1 X2, gmm(X1) iv(zk allempl_arvenum X2) noc robust
est store G1
outreg2 using 2digit3.xls, cttop(GMM:C`i') e(all) append
outreg2 using 2digitnoa3.xls, cttop(GMM:C`i') noas append
restore
}
已有 1 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
Sunknownay + 100 + 10 + 1 + 1 + 1 热心帮助其他会员

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

藤椅
sungmoo 发表于 2012-4-19 05:53:06
*其他帖中已有相关讨论
egen g=group(industry)
su g
loc n=r(max)
forv i=1/`n'{
*循环中把红字 191 换成  `i'  即可。
}
已有 1 人评分论坛币 热心指数 收起 理由
dxystata + 20 + 1 好的意见建议

总评分: 论坛币 + 20  热心指数 + 1   查看全部评分

板凳
orochieh 发表于 2012-4-19 07:04:46
受益了,赶紧学了,哈哈哈哈
[b]万物并作 吾以观复[/b]

报纸
weekendxzr 在职认证  发表于 2012-4-20 18:58:46
sungmoo 发表于 2012-4-19 05:53
*其他帖中已有相关讨论
egen g=group(industry)
su g
还是不太明白啊,我之前的程序是贴在哪里呢?
是放在大括号里面吗?
但是还有一个问题,因为我每一次执行回归的时候都选择Keep某个行业的数据来做,等于把其他行业就删掉了。执行完了以后下次再回归都是clear all 然后再导入数据再做。

这样一循环就出问题了,因为你那个g生成是建立在数据已经导入数据的情况下啊,有没有什么办法不需要删除其他行业,直接对某个行业进行回归呢?

地板
weekendxzr 在职认证  发表于 2012-4-20 19:01:24
sungmoo 发表于 2012-4-19 05:53
*其他帖中已有相关讨论
egen g=group(industry)
su g
clear all
set more off
cap log close
cd "C:\Users\Administrator\Desktop\新建文件夹"
use "新新2位数行业新数据.dta", clear
egen g=group(businessnature4)
su g
loc n=r(max)
forv i=1/`n'{
keep if businessnature4==`i'
xtbalance, range(2005 2007)
reg Y2 X1 X2, noc robust
est store R1
outreg2 using 2digit3.xls, cttop(OLS:C`i') e(all) append
outreg2 using 2digitnoa3.xls, cttop(OLS:C`i') noas append
xtabond2 Y2 X1 X2, gmm(X1) iv(zk allempl_arvenum X2) noc robust
est store G1
outreg2 using 2digit3.xls, cttop(GMM:C`i') e(all) append
outreg2 using 2digitnoa3.xls, cttop(GMM:C`i') noas append
}
上面这个程序,是这样吗?
但是你看循环语句里面,那个Keep实际上已经把其他行业的数据删掉了,怎么样再下次循环的时候把其他行业的数据找回来,或者有什么办法不需要删除其他行业呢?

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

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