【问题标题】:How to loop through multiple Excel files in Stata?如何循环遍历Stata中的多个Excel文件?
【发布时间】:2014-07-11 16:47:06
【问题描述】:

这是我的代码:

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

但是当我不包含该行时,它会给我 this 错误:

using required

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

【问题讨论】:

    标签: excel for-loop stata


    【解决方案1】:

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

    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找不到该文件。这是由于您声明宏列表然后用双引号调用的方式。 help quotes 是相关阅读这里。

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

    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,包括在内。一个参考是 Stata tip 65: Beware the backstabbing backslash Nicholas J. Cox.

    至于using required 错误,我猜这与import excel 期望字符串而不是代数表达式 有关。在命令中添加一个简单的+ 将重现您的错误。例如:

    .   import excel "V:/Report07" +
    using required
    r(100);
    

    Stata 不理解该符号,因此它会抱怨;它需要一些代表文件路径的字符串。 (如果您运行 import excel "V:/Report07 +",则会发生不同的情况。

    【讨论】:

      【解决方案2】:

      尝试修改您的“导入 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”。

      【讨论】:

      • 反引号前的反斜杠在 Stata 中是一个坏主意,因为使用反斜杠作为转义字符与在 Windows 文件路径中作为分隔符的使用相冲突。在 Windows 文件路径中使用正斜杠; Stata 将为您翻译。记录在手册和stata-journal.com/sjpdf.html?articlenum=pr0042
      【解决方案3】:

      我知道这已经 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 
      

      我不知道反斜杠问题。

      【讨论】:

      • 论坛软件吃了你需要显示的反斜杠来解释你解决的问题。现已修复。
      猜你喜欢
      • 2019-07-21
      • 2019-01-03
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多