【问题标题】:Adding time in a file name在文件名中添加时间
【发布时间】:2017-05-21 21:46:05
【问题描述】:

考虑下面的代码 sn-p:

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?

【问题讨论】:

    标签: stata stata-macros


    【解决方案1】:

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

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

    关于您的第二个问题,只需将 \ 替换为您的 带有/ 的文件路径。

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

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多