I frequently need to access files on a shared network that for reasons lost to time uses names of months as part of the file hierarchy (e.g. //drive/2020/January/filename.xls). I frequently need to access/copy files from the prior month (relative to the time at which I am running an analysis), and I'd like to use a dynamic method to do so. In reading the Stata documentation for datetime formats, it seems like using local macros based on c(current_date) is one way to accomplish this.
However, when I use the following code to obtain the name of the prior month relative to now (November), there ends up being an embedded leading space, which errors out when included in file paths:
Code:
clear all local last_month = dofm(mofd(daily(`"`c(current_date)'"', "DMY"))) - 1 local Month :display %tdMonth `last_month' di "`Month'"
. local last_month = dofm(mofd(daily(`"`c(current_date)'"', "DMY"))) - 1
. local Month :display %tdMonth `last_month'
. di "`Month'"
November
I worked around this by using the strtrim() function:
Code:
local MonthTrimmed = strtrim("`Month'") di "`MonthTrimmed'"
. local MonthTrimmed = strtrim("`Month'")
. di "`MonthTrimmed'"
November
. macro list _Month _MonthTrimmed
_Month: November
_MonthTrimmed: November
My questions are:
- Is there something in the syntax of the command local Month :display %tdMonth `last_month' that is resulting in the embedded space (i.e. am I inadvertently embedding a leading space by the command)? I tried different iterations of compound quotes, but I didn't have success in getting rid of (or failing to include) the space before November.
- Is there a more preferred approach to get what I really am after, which is the name of the month immediately prior to this month, for use later in do files?
0 Response to Syntax for date format local macros based on c-class date value resulting in embedded leading space
Post a Comment