Announcement

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

  • Local in Global Directory Path

    I am trying to define a global directory path with local string in it and then load data using this directory path. But stata gives me an error: invalid 'Finland'. When I display the global path, I get "C:/Users/me/Desktop/Orbis/Analysis/Finland"". So I think something is wrong how I define the global path. Any help is greatly appreciated. I Here is the code:

    Code:
    foreach j in "Finland" "Spain" "Germany" {
    global mainpath "C:/Users/me/Desktop/Orbis/Analysis/"`j'""
    use "${mainpath}\Data\"`j'"_clean_nofire.dta", clear
    }

  • #2
    Try

    Code:
     
     foreach j in "Finland" "Spain" "Germany" {  global mainpath "C:/Users/me/Desktop/Orbis/Analysis/`j'"  use "${mainpath}\Data/`j'_clean_nofire.dta", clear  }
    Two likely errors corrected:

    1. Your "" are in place. You don't need more.

    2. A backslash should never be followed by a local macro reference.

    Comment


    • #3
      Code:
      global mainpath "C:/Users/me/Desktop/Orbis/Analysis/`j'"
      The problem arose from the quotation marks around `j' in the global. It resulted in the malformed expression ""C:/Users/me/Desktop/Orbis/Analysis/Finland"" --note the unbalanced quotes. Even if those quotes were balanced, they would not work the way you are thinking here. When "something"something else"" is encountered this is parsed as (something) (something else), not as (something(something else)). For nested quotes, you have to use the compound double quotes (`" "') . But no quotes are needed here anyway, and, as you can see, they can get in the way because Stata has a tendency to strip off initial quotes.

      By the way, you also don't need the quoets around the country names in the -foreach- statement, although in this instance they aren't causing any problems. In general, in Stata, you should avoid quotes where they aren't definitely needed: they sometimes make problems. I understand, in your context, that there are country names like "South Africa" with embedded blanks that need to be enclosed in quotes. But I urge you to just selectively put quotes around those as they appear, and not do it routinely where they are not required.

      Finally, you should not use a global macro to define a directory path. Global macros are an inherently unsafe storage mechanism because they can clash with other global macros of the same name in other programs that may be invoked in your own program. So, if your program calls another program that has a global macro mainpath (and you may not even know that your program does this because it might be called by a program that is called by a program you call, and you don't know the full chain), and if that program changes the value of mainpath, then when your program resumes, mainpath has, invisibly, changed and the rest of your program's execution depending on that value will be incorrect. This situation doesn't arise often, but when it does, finding and fixing the source of the problem is so intensely frustrating and difficult that you will never, ever, want to have to do it again. The best bet is to usually use local macros--these go out of scope when execution passes to a different program, so they cannot be covertly changed. Sometimes there is need to pass information across program boundaries that is not feasible to do as program arguments or options. For that there may be other safer options like putting the information in a variable, or in a -note-. Those, too, can be clobbered by another program, but few programs actually use -notes-, so the risk is lower. If none of those are available, a global macro is a last resort--but you should choose its name carefully so that the likelihood of a name clash is minimized. For example, choose a name that's like a strong password! I have been using Stata since 1994, and I use it nearly every single day in my work. In all of that time, I have only once had to resort to a global macro.

      Added: Crossed with #2.

      Comment


      • #4
        Thank you both. This is really helpful! I tried your suggested solution before but with back slashes instead of forward slashes and it didn't work. It's amazing how sensitive stata is to the direction of slashes. But it's really good to know for the future.

        Clyde Schechter the main reason why I am using the global to define directory paths is because I am using it in multiple do-files. How would you define and call a directory path used in multiple do-files with a note?

        Comment


        • #5
          Well, for that purpose, there is an even better solution than a note. Create a do-file, let's call it pathfile.do, that contains
          Code:
          local mainpath "C:/Users/me/Desktop/Orbis/Analysis/"
          and save it in your working directory.

          Then in each do-file that will need that information you put a line:
          Code:
          include pathfile.do
          The -include- command (do read -help include- for the full story) causes the contents of pathfile.do to be inserted into the do-file where the -include- command is. So it is not like calling the other do-file, because its contents are directly copied, and local defined in the -include-d file are thereby defined in the file that contains the -include-.

          For example, your code in #1 becomes:

          Code:
          include pathfile.do
          foreach j in Finland Spain Germany {
              local curent_path `mainpath'/`j'
              use "`current_path'/Data/`j'_clean_nofire.dta", clear
          }

          Comment


          • #6
            Awesome thanks Clyde Schechter !

            Comment

            Working...
            X