Announcement

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

  • global filepaths/close-quote

    Hello,

    Is there a way to "close quote" a global macro? I'm trying to create a global macro filepath, but not being able to distinguish where the path ends and the filename begins, makes stata think the entire thing is the global. There must be a simple solution to this, but I haven't been able to find it. Thanks in advance for any help.

    Example:

    Code:
    global path I:\datasets\good_data\
    
    use $pathdataset.dta
    whereas if local, it's clear:

    Code:
    use `path'dataset.dta
    Thanks Statalist!
    -Reese

    p.s. I'm using version 14

  • #2
    Curly braces:
    ${path}dataset.dta
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Thanks Eric!

      Comment


      • #4
        Code:
        use ${path}dataset.dta
        But really, that's just another reason you should use a local macro instead of global anyway. Global macros are an inherently unsafe programming practice and should be avoided except where there is absolutely no reasonable alternative. Storing pathnames is not one of those exceptions. (They are so rare that, using Stata since 1994 on a daily basis, I have only used a global macro twice--and it still bothers me that I couldn't find a way around those.)

        The excuses often given for using global macros when a local will work typically don't hold water:

        1. "I have to define the macro near the top of the code, but I now want to run only a block of code that is later in the do-file without running everything in between." Well, just comment-out the code in between. (Which is super-easy if you have version 15. Select the code in between and press Ctrl+/ (in Windows, I'm sure there's something analogous in Mac).

        2. "I'm writing a large number of do-files to run consecutively and they all need to use this same pathname. A local macro won't carry over from one do-file to the next." Well, that is why there is an -include- command. Create a new do-file that defines a local macro with this pathname (and any other local macros that are common to all those do-files). Let's call that file common_macros.do. Then in each file that needs to use all these common local macros, insert a line

        Code:
        include common_macros.do
        Added: Crossed with #2 and #3.

        Comment

        Working...
        X