如何循环 excel sheet 并在 Stata 中列出?
How to loop over excel sheet and lists in Stata?
我刚开始使用 Stata,我无法弄清楚以下内容。
如何遍历 Excel 工作表和索引的列表。这现在工作正常。
clear all
set more off
local mysheets 1996 2000 2003 2007 2008 2010
local indices index1 index2 index3
foreach sheetname of local mysheets {
import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear
foreach index of local indices{
tobit theta index, ll(0) ul(1)
outreg using "C:\stata\results.doc" , `append'
local append "append"
}
}
只是为了 post 作为答案(所以问题不会出现未回答),因为它似乎是一个简单的编码错误:
- 确保局部宏名称始终一致(
mysheet
与 mysheets
)
- 在
foreach
循环 中对 foreach
(在本例中为 sheetname
)的参数使用本地宏语法
如果使用局部宏来定义outreg
的append
选项,则在调用该选项之前定义它
clear all
set more off
local mysheets 1996 2000 2003 2007 2008 2010
local indices index1 index2 index3
foreach sheetname of local mysheets {
import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear
foreach index of local indices {
tobit theta `index', ll(0) ul(1)
local append "append"
outreg using "C:\stata\results.doc" , `append'
}
}
我刚开始使用 Stata,我无法弄清楚以下内容。
如何遍历 Excel 工作表和索引的列表。这现在工作正常。
clear all set more off local mysheets 1996 2000 2003 2007 2008 2010 local indices index1 index2 index3 foreach sheetname of local mysheets { import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear foreach index of local indices{ tobit theta index, ll(0) ul(1) outreg using "C:\stata\results.doc" , `append' local append "append" } }
只是为了 post 作为答案(所以问题不会出现未回答),因为它似乎是一个简单的编码错误:
- 确保局部宏名称始终一致(
mysheet
与mysheets
) - 在
foreach
循环 中对 如果使用局部宏来定义
outreg
的append
选项,则在调用该选项之前定义它clear all set more off local mysheets 1996 2000 2003 2007 2008 2010 local indices index1 index2 index3 foreach sheetname of local mysheets { import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear foreach index of local indices { tobit theta `index', ll(0) ul(1) local append "append" outreg using "C:\stata\results.doc" , `append' } }
foreach
(在本例中为 sheetname
)的参数使用本地宏语法