全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
3310 8
2014-05-03
在用到数据处理的时候需要两个loop一起用,也就是一个大的loop里面包着一个小的loop,不知道stata可以实现吗?做了一下发现语法错误什么的,请教下各位大神:
我的是这样:
capture program drop x
program x
forvalues i = 1 (1) 6 {
xxxx
xxxx
xxxx
forvalues j = 1 (1) 8{
XXX
XXX
}
}
end
x
这样对不对?

二维码

扫码加我 拉你入群

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

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

全部回复
2014-5-3 21:08:26
暂时帮不到你,抱歉
二维码

扫码加我 拉你入群

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

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

2014-5-3 21:12:08
数着星星玩 发表于 2014-5-3 21:08
暂时帮不到你,抱歉
恩 谢谢
二维码

扫码加我 拉你入群

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

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

2014-5-3 21:21:46
这样的程序肯定是没错的,至于为什么语法错误,要把代码贴出来才能分析出来
复制代码
运行结果:
output.jpg
二维码

扫码加我 拉你入群

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

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

2014-5-3 21:27:21
╰不滅信念 发表于 2014-5-3 21:21
这样的程序肯定是没错的,至于为什么语法错误,要把代码贴出来才能分析出来
运行结果:
capture program drop x

program x

forvalue i = 2 (i) 6 {

use "e:\match.dta",clear

sort Stkcd Trddt

bys Stkcd: gen datenum=_n

by Stkcd: gen target_1=datenum if i_n==`i'

egen td = min (target_1),by(Stkcd)

drop target_1

*find the interval day
gen dif=datenum-td

*choose the length of the event window
by Stkcd: gen event_window=1 if dif>=0 & dif <=21

forvalues j = 2(1)36{

by Stkcd: replace event_window=`j' if dif>=(`j'-1)*21+(`j'-1) & dif <=`j'*21+(`j'-1)
}

*caculate the length of the window for every company
egen count_event_obs=count(event_window),by(Stkcd)

sort Stkcd event_window

keep if  event_window !=.

keep if  count_event_obs==792

count if i_m==1

sort Stkcd Trddt

*2.3 caculate the buy and hold stock return
sort Stkcd event_window Trddt

by Stkcd event_window: gen return_stock=(Clsprc[_N]-Clsprc[1])/Clsprc[1]

by Stkcd event_window: gen return_stock1=return_stock+1

sort Stkcd event_window Trddt

by Stkcd event_window: gen return_stock2=return_stock1 if i_m ==1

by Stkcd event_window: replace return_stock2=1 if return_stock2==.

egen R1=prod(return_stock2),by(Stkcd)

keep if i_n==`i'

egen r1_sum = sum(R1)

saveold "E:\1992_nonipo_`i'",replace

}
end

x


程序是这样的,子LOOP是用来生成窗口的,母的LOOP是用来选择股票的,分别是2 ,3, 4 到6
二维码

扫码加我 拉你入群

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

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

2014-5-3 21:27:52
╰不滅信念 发表于 2014-5-3 21:21
这样的程序肯定是没错的,至于为什么语法错误,要把代码贴出来才能分析出来
运行结果:
先谢过!~~
二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群