简体   繁体   English

如何在Stata中循环浏览Excel工作表和列表?

[英]How to loop over excel sheet and lists in Stata?

I just started working with Stata and I couldn't figure out the following. 我刚开始与Stata合作,但我不知道以下几点。

  1. How can I loop over the lists of Excel sheets and the indices. 如何遍历Excel工作表列表和索引。 This works fine now. 现在工作正常。

     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" } } 

Just to post as an answer (so the question doesn't appear unanswered), since it seems to be a simple coding error: 只是作为答案发布(这样就不会出现未回答的问题),因为这似乎是一个简单的编码错误:

  • Make sure local macro names are consistent throughout ( mysheet vs. mysheets ) 确保本地宏名称在整个过程中保持一致( mysheetmysheets
  • Use local macro syntax for the argument of foreach (in this case, sheetname ) inside of foreach loop foreach循环内,将本地宏语法用作foreach的参数(在本例中为sheetname
  • If using a local macro to define the append option of outreg , define it before the option is called 如果使用本地宏定义outregappend选项, outreg在调用该选项之前对其进行定义

     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' } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM