Stata year-quarter for loop

半世苍凉 提交于 2019-12-12 01:20:03

问题


I am trying to create a loop in Stata. I run a model for the data <= year and <= quarter. Then predict one year look ahead. That is the model is run all time points upto the loop, while the prediction happens in the next quarter out of sample. So my question is how do I handle so that when yridx = 2000, and qtr = 4, the next quarter inside the loop look ahead would be year = 2005, and year = 1.

foreach yridx of numlist 2000/2012 {
forvalues qtridx = 1/4 {

regress Y X if year <= yridx and qtr <= qtridx
predict
}
}

回答1:


It sounds as if it would be much easier to work in terms of quarterly dates. Here is one of several ways to do it.

gen qdate = yq(year, qtridx) 
forval m = `=yq(2000,1)'/`=yq(2012, 4)' { 
    regress Y X if qdate <= `m' 
    predict <whatever> 
}


来源:https://stackoverflow.com/questions/35665993/stata-year-quarter-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!