Add current date to Stata file names (and others)

We wrote earlier about how to add time stamps (current date and/or time) to your Stata figures, e.g. to keep track of work in progress results. A similar strategy can be used when storing log-files, which are typically overwritten with a more recent version of the same log.

Because the built-in macros $S_TIME and $S_DATE (and `c(current_time)' and `c(current_date)') contain spaces, a simple way to store the current date without spaces is the following:

local logdate : di %tdCYND daily("$S_DATE", "DMY")

The local logdate can then, for example, be used to store the day-specific log file (optimally in a separate log folder)

log using "${DIR_LOG}/log_file_`logdate'", text

Leave a Reply