Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to deal with December/January issues when using mkdir to create directories from system dates

    Hello all,

    Using Stata 15.1/IC

    I've been using a do file for the past 8 months or so that automatically creates folders using the mkdircommand with system dates. I run the file once a month. The data are from a database as of the prior month end, so I desire to create a folder directory structure that would correspond to the year of the prior month and a subfolder with the name of the prior month. The code I've been using is:

    Code:
    **Create project directory for last month's data files
    local path "C:/Users/Project" //Project Directory
    local lastmonth = word(c(Months), (month(td(`c(current_date)')))-1) //Name of the month prior to this month--does not work in January
    local year = (year(td(`c(current_date)'))) //Current year
    
    cd "`path'"
    capture mkdir "`year'/`lastmonth'"
    For example, if I run this in November 2018, this would create the directory "C:/Users/Project/2018/October" and the rest of my do file ends up exporting files to this folder.

    This works in all months of the year except for January. This is because the local for the name of the prior month (local lastmonth above) stores the name of the month that corresponds to the nth-1 word of the system generated c(Months) list. Because it's now January, it's storing nothing because there is no 0th month of the year. And the local year macro is simply the current year, so this wouldn't be correct in 2019 (since I would want the year of the prior month, in this case 2018).

    The adjustments to the macros are probably not too hard to change to get it to return what I need for January 2019 (i.e. "C:/User/Project/2018/December"). Lesson learned: I should have realized that because something worked several times, it's not guaranteed to work in all cases.

    I'd really appreciate some guidance on what commands (or approaches) would work year round, where no adjustments would need to be made to the do file for any of the months. Thanks!

  • #2
    There may be a more elegant or simpler way to do this, but I think what you really want is to calculate a Stata internal format monthly date from the current date, and subtract one from that, then convert it to CCYY/Month display format:

    Code:
    local last_month = mofd(daily(`"`c(current_date)'"', "DMY")) - 1
    local ym  :display %tmCCYY/Month `last_month'
    display `"`ym'"'
    cd "`path'"
    capture mkdir "`ym''"
    This requires no special workaround for December/January.

    Comment


    • #3
      Clyde Schechter : I like this approach very much--however, when I tried using it I am getting an error (after removing capture so as to test):

      Code:
      . local path "C:/Users/jimst/Project"
      
      . local last_month = mofd(daily(`"`c(current_date)'"', "DMY")) - 1
      
      . local ym  :display %tmCCYY/Month `last_month'
      
      . display `"`ym'"'
       2018/December
      
      . cd "`path'"
      C:\Users\jimst\Project
      
      .  mkdir "`ym''"
      could not create directory  2018/December'
      r(693);
      I think it has something to do with the macro including a leading space before the year. I tried including single quotes like in the display `"` ym '"' command above, but Stata threw the same error. I also tried including the full path but got the same error:

      Code:
      . local path "C:/Users/jimst/Project"
      
      . local last_month = mofd(daily(`"`c(current_date)'"', "DMY")) - 1
      
      . local ym  :display %tmCCYY/Month `last_month'
      
      . display `"`ym'"'
       2018/December
      
      . cd "`path'"
      C:\Users\jimst\Project
      
      .  mkdir "`path'/`ym''"
      could not create directory C:/Users/jimst/Project/ 2018/December'
      r(693);
      Thoughts? I have admin permissions to create files in the directory, so that shouldn't be an issue. I also removed the extra close single quote at the end of the ym macro, but same error.

      Comment


      • #4
        Check to see if it is the extra space in front of 2018:
        Code:
        local last_month = mofd(daily(`"`c(current_date)'"', "DMY")) - 1
        display `last_month'
        local ym  :display %tmCCYY/Month `last_month'
        local ym = strtrim("`ym'")  // this should remove that leading space
        display `"`ym'"'

        Comment


        • #5
          You have too many right quote marks. The last one should go.

          Comment


          • #6
            In addition to what David and Nick point out, I believe that you need to make the nested subdirectories successively. You cannot create a year subdirectory and its month subdirectory in one fell swoop.

            Try something like the following.
            Code:
            local path C:/Users/jimst/Project/
            
            local last_month = mofd(date(c(current_date), "DMY")) - 1
            local ym : display %tmCCYY_Month `last_month'
            gettoken year month : ym
            
            // Create year subdirectory first
            mkdir `path'`year'
            
            // Then create month subdirectory inside of now-existing year subdirectory
            local directory `path'`year'/`month'
            local directory : subinstr local directory " " "", all
            
            mkdir "`directory'"
            You can create a complete path with md from the DOS prompt, but you cannot using the New folder icon from the Windows File Explorer program. I think that Stata follows the latter's restrictions.

            Comment


            • #7
              Thank you to all. To close out the thread, this is the code that executed correctly:

              Code:
              local last_month = mofd(daily(`"`c(current_date)'"', "DMY")) - 1
              local ym  :display %tmCCYY/Month `last_month'
              local ym = strtrim("`ym'")
              display `"`ym'"'
              cd "`path'"
              mkdir "`ym'"

              Comment

              Working...
              X