Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Stata中循环浏览多个Excel文件?_Excel_For Loop_Stata - Fatal编程技术网

如何在Stata中循环浏览多个Excel文件?

如何在Stata中循环浏览多个Excel文件?,excel,for-loop,stata,Excel,For Loop,Stata,以下是我的代码: local dateList "01" "02" "03" "04" "05" "06" "07" foreach date of local dateList { use `"`date'"' clear import excel "V:\Report07" + `"`date'"' + "13.xls", sheet("MySheet") firstrow sort PersonID Place bysort PersonID (Place):

以下是我的代码:

local dateList "01" "02" "03" "04" "05" "06" "07" 

foreach date of local dateList {
  use `"`date'"'

  clear

  import excel "V:\Report07" + `"`date'"' + "13.xls", sheet("MySheet") firstrow

  sort PersonID Place
  bysort PersonID (Place): gen mix = Place[1] != Place[_n]
  sort PersonID
  by PersonID: egen anymix=max(mix)

  count if anymix==1

  drop mix
  drop anymix
}
我正在尝试循环浏览多个因日期不同而不同的Excel文件,正如您在我放置变量
date
的代码中看到的那样。例如,此电子表格的名称为
Report070113
,表示2013年7月1日的日期。下一次通过循环时,应导入标题为
Report070213
的报告。我认为最好的方法是创建一个字符串数组,这些字符串是一个月的不同日期,这样我可以逐月运行代码,并获得每个访问过不同地方的人的计数。我知道循环中的内容工作正常,但for循环本身有问题。当我有:

use `"`date'"'
在代码(第3行)中,它给出了以下错误:

file 01".dta not found
但当我不包括这一行时,它会给我一个错误:


任何帮助都将不胜感激;如果我的问题不清楚,请告诉我。

查看使用单引号和双引号之间的区别:

clear all
set more off

local dateList "01" "02" "03" "04" "05" "06" "07" 

foreach date of local dateList {
  disp `"`date'"'
  disp "`date'"
}
问题是宏列表的第一个元素是
01“
,而不是
01
。因此Stata找不到该文件。这是由于您声明宏列表,然后用双引号调用的方式造成的。
帮助引号
是此处的相关内容

对我来说,这样做会更清楚:

clear all
set more off

local dateList 01 02 03 04 05 06 07

foreach date of local dateList {
  disp `"`date'"'
  disp "`date'"  
  display "V:/Report07`date'13.xls"

  * This should work for you (uncomment)
  *import excel "V:/Report07`date'13.xls", sheet("MySheet") firstrow
}
注意:宏的声明不包含引号。使用双引号或单引号的调用现在会给出相同的结果。为源文件创建字符串非常简单。此外,建议使用
/
而不是
\
。这使代码在其他操作系统之间兼容。Stata将使其适用于MS Windows,包含在内。引用为 尼古拉斯·J·考克斯著

至于
使用required
错误,我猜这与
导入excel
需要字符串而不是代数表达式有关。向命令添加简单的
+
将重现错误。例如:

.   import excel "V:/Report07" +
using required
r(100);
Stata不理解符号,因此它抱怨;它需要一些表示文件路径的字符串。(如果运行
import excel“V:/Report07+”
,则会出现不同的情况。

尝试修改“import excel”文件路径:

local dateList "01" "02" "03" "04" "05" "06" "07" 

foreach date of local dateList {

  clear

  import excel "V:\Report07\`date'13.xls", sheet("MySheet") firstrow

  sort PersonID Place
  bysort PersonID (Place): gen mix = Place[1] != Place[_n]
  sort PersonID
  by PersonID: egen anymix=max(mix)

  count if anymix==1

  drop mix
  drop anymix

}

如果它仍然要求“使用必需的”,那么请尝试“使用导入excel…”。

我知道这已经3年了,但我在尝试循环打开和修改多个excel文件时遇到了相同的问题(“使用必需的”错误)。更改目录名中斜杠的方向修复了该问题

local yearlist 2014 2015

foreach year of local yearlist { 

        import....
        [do data cleaning]
        save update file
}
原始(失败)导入命令:

 import excel "Z:\Data\`year'\GIS Class Data Report1_`year'.xlsx", sheet("Sheet1") firstrow clear 
新建(工作)导入命令:

 import excel "Z:/Data/`year'/GIS Class Data Report1_`year'.xlsx", sheet("Sheet1") firstrow clear 

我不知道反斜杠的问题。

反引号之前的反斜杠在Stata中是个坏主意,因为反斜杠作为转义字符的使用与其在Windows文件路径中作为分隔符的使用相冲突。在Windows文件路径中使用正斜杠;Stata将为您翻译。在手册和论坛软件中记录了反斜杠你需要展示的灰烬来解释你解决的问题。现在已修复。
 import excel "Z:/Data/`year'/GIS Class Data Report1_`year'.xlsx", sheet("Sheet1") firstrow clear