1

I have the following code:

local date "September"

global dir `c(pwd)'
global files "A B C" 

foreach x of global files { 
    import excel "${path}`x'_`date'.xlsx", sheet("1") cellrange(A3:O21) clear 
    generate Store="`x'"
    save `x', replace
}

The problem is that not all my files have the date September. Some have August or May.

How can I incorporate a solution for this in the above script?

The idea is that if September is not found, the code would still run. However, instead of September it would go for August and if that fails, then May.

4

1 回答 1

2

假设以下文件存储在您的工作目录中:

A_September.xlsx
B_August.xlsx
C_May.xlsx

您可以使用宏扩展函数dir和通配符来创建本地宏files,其中将包含符合条件的文件列表:

local files : dir "`c(pwd)'" files "*_*.xlsx"                               

foreach x of local files {
    display "`x'"
}

A_September.xlsx
B_August.xlsx
C_May.xlsx

在 Stata 的命令提示符下键入help extended_fcn会为您提供更多信息。

现在假设在您的工作目录中有两个附加文件:

ASeptember_34.xlsx
C_May45.xlsx

在这种情况下,文件将包含在列表中:

local files : dir "`c(pwd)'" files "*_*.xlsx"                               

foreach x of local files {
    display "`x'"
}

A_September.xlsx
ASeptember_34.xlsx
B_August.xlsx
C_May.xlsx
C_May45.xlsx

为了在循环中忽略这些,您需要使用正则表达式进一步过滤文件名:

local files : dir "`c(pwd)'" files "*_*.xlsx"                               

foreach x of local files {
    if ustrregexm("`x'", "[A-Z]_([A-Z][a-z]+).xlsx") display "`x'"
}

A_September.xlsx
B_August.xlsx
C_May.xlsx

请注意,所需正则表达式的复杂性将取决于工作目录中包含的文件名的模式。

于 2018-12-09T13:00:15.640 回答