在文件名中添加时间

Adding time in a file name

考虑下面的代码片段:

local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), " " , "", .)
save "`date_string'_example", replace
mkdir "`date_string'_example"

这将创建如下输出:

20170521_example.dta

但是,我想创建一个包含时间的文件名,而不仅仅是日期。如果时间是下午 4 点 25 分 01 秒,那么我希望有以下内容:

20170521_162601_example.dta

如何添加这个元素?

我尝试将 c(current_time) 放入 date() 函数中,但没有成功。

此外,我尝试使用 date_string 分配本地宏 savedir。但是后面跟\:

的时候Stata好像认不出来了
local date: display %td_CCYY_NN_DD date(c(current_date), "DMY")
local date_string = subinstr(trim("`date'"), " " , "", .)
local savedir "C:\Users\`date_string'_output"
cd "`savedir'

如何保存本地宏savedir

你可以得到想要的输出如下:

local datetime_string : display %tc_CCYYNNDD_HHMMSS clock(c(current_date) + " " + ///
                         c(current_time), "DMYhms")

关于你的第二个问题,把你的\换成 / 的文件路径。

正斜杠可以在 Stata 中使用 对于所有操作系统上的文件路径,Stata 将负责翻译 他们在 Windows.

上正确