首页 > 解决方案 > stata:保存时工作表名称不存在

问题描述

我正在编写一个访问 excel 文件中的多个工作表的 stata 程序,在对它们进行操作之后,我尝试保存结果,但它显示工作表名称不存在(Cabo not found, r(111))我的代码是这样的:

clear
foreach sheet in "Cabo" "Ga" "Inha" {
import excel using "filename.xlsx", sheet("`sheet'") firstrow
//here are operations//
if `sheet' == "Cabo" {
    save test1018
    }
else {
    append using test1018.dta
    save test1018
    }
}

有谁知道我做错了什么?谢谢!!

标签: excelstata

解决方案


, replace在追加后保存数据时,您还需要包含一个for。您还可以通过在循环之前保存一个空数据集来简化代码。然后,每个工作表都将附加到此数据集,您不必将第一个工作表与后续工作表区别对待。

*Save an empty dataset to append to
clear
save test1018.dta, replace emptyok

foreach sheet in "Cabo" "Ga" "Inha" {
    import excel using "filename.xlsx", sheet("`sheet'") firstrow
    //here are operations//
    append using test1018.dta
    save test1018.dta, replace
}

推荐阅读