Announcement

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

  • Questions regarding path macros

    Hi

    I came across the following path macros in a do file template. This is for a dropbox.

    global dta "$path\dta"
    global dta_source "$path\dta_source"
    global log "$path\log"
    global log "$path\graphs"
    *-------------------------------------------------------------------------------


    capture log close
    set logtype text
    log using "${path}\log\log_NAME.log", replace
    cd "${path}"

    I was wondering what global dta and global log refer to and how I should modify them. In addition, I am also a bit confused about the last few lines. Will anyone have any clue?

    Thank you!

  • #2
    The first four lines of code define four global macros. (I think there is an error in the fourth line, as it re-defines the same global macro that was defined in the previous line. I suspect that line was meant to be -global graphs "$path\graphs". All of these global macros contain the names of subdirectories of some directory which is defined by the contents of global macro path (which you don't show us). So these are just a way of creating a short easy way to refer to these subdirectories when needed.

    In the subsequent code, -capture log close- closes any log file that may be lying around open at the time it is executed. The lines
    Code:
    log using "${path}\log\log_NAME.log", replace
    cd "${path}"
    tell Stata to open a log file named log_NAME.log in the log subdirectory of the directory specified in ${path}. Note, by the way, that having already defined global macro log to be $path\log, the first of these two lines could be written more compactly as:

    Code:
    log using "${log}\log_NAME.log". replace
    Finally, there are a couple of bad practices in this code that I would recommend you change unless you are required to follow this template.

    1. In pathnames, it is better to use / then \ as the separator. Even though in Windows, the path separator is suposed to be /, Stata commands that handle filenames and paths understand that in this context / is to be understood as \. The reason using \ is bad is that \ sometimes introduces an escape sequence, so that \ plus the next character gets misinterpreted. Another advantage of using / regardless of operating system is that if you ever need to port the code to Unix or Mac, it will be usable as is if you use / as the separator..

    2. Global macros really shouldn't be used for this sort of thing. Global macros are subject to name clashes with global macros defined in other programs. When you run your program, you may not even be aware of what other programs are running or will start to run during your session, so defining a global macro can have unpredictable consequences. Similarly, if another program redefines a global macro with the same name, that will affect the performance of your program in unpredictable ways. So if I were writing all of this coode, I would use local macros instead. These are completely safe because they have no scope outside the current do file. So if I were doing this, it would look like this:

    File path_definitions.do (saved separately)
    Code:
    local path whatever // PUT CONTENTS OF $path here
    local path dta `path'/dta
    local path dta_source `path'/dta_source
    local path log `path'/log
    local path graphs `path'/graphs
    Current do-file
    Code:
    capture log close
    set logtype text
    include path_definitions
    log using "`log'/log_NAME.log", replace
    cd "`path'"

    Comment


    • #3
      Thanks so much for the kind assistance!

      Comment

      Working...
      X