Announcement

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

  • Using a global macro to open a .dta file

    Dear stata users.

    I have been having a problem when I try to open a .dta file when using a global macro.

    For example, I want to open the .dta file named "canada2" :

    global country canada

    use "$country2"

    returns an error since stata interprets $country2 as an (empty) global that is being called.

    How can I get around this problem?

    Thank you for your help.

  • #2
    Your use command is looking for a global variable called country2. For what you want to accomplish,
    Code:
    use ${country}2

    Comment


    • #3
      Thank you very much

      Comment


      • #4
        William's answer is correct. But, really, you shouldn't use a global macro for this purpose in the first place. Global macros are an inherently dangerous programming technique. When you create the global macro country, you have no way of knowing whether some other program that is lurking in the background already has a global macro by that name: if it does, you've just clobbered it and will likely break that program. Worse still, you have even less possibility of knowing whether some other program that you will call in later uses a global macro with that name--and it will then clobber yours. The bizarre behavior that can result from this kind of thing is very difficult to debug. It is best avoided. Use a local macro under all possible circumstances. If you think about what you are doing you will find that there are very, very few situations where global macros are truly necessary. (I've been using Stata since 1994 and have only resorted to using a global macro twice.) Local macros are safe because they have no scope outside the program that defines them, so the kind of name clashes that are an inherent risk of global macros are impossible with locals.

        Let me point out two circumstances that people often cite as reasons to use global macros, neither of which is actually a good reason:

        1. The local macros are defined near the beginning of a do-file, but you want to only run a portion of the do file that is farther down and refers to those macros. The solution is to stick with local macros and comment out the intervening code segments. Stata has made that even easier now: in version 14.2 you can just highlight the segments of code you want to comment out and press CTRL-/ (in Windows, there's an analogous keystroke combination on the Mac and in Linux as well.) When you're done with that and want to un-comment them, highlight again, press CTRL-/ again, and the commenting goes away.

        2. There is a series of definitions to be used by many different files, such as certain filenames or directory paths, and you want to just define them once and for all. Again, there is no need for global macros here. In fact, the global macro solution has an additional danger: it only actually works if you are sure that you will always run the file that defines those global macros before you run any of the files that use them. The solution is to put the local macros in a separate do-file of their own. For the sake of illustration, let's call it my_local_macros.do. Then in any do-file where you want to make use of those local macros, write the command -include my_local_macros-. The -include- command causes Stata to read and execute all commands in the included file as if they were part of the including file. So the local macros have scope in the file that -include-s the other one. (Note that -run- or -do- does not accomplish this; only -include- will work.)

        Comment


        • #5
          Thank you very much for your reply Clyde.

          As a new user to Stata I had no idea of the problems associated with the use of global macros. You just improved the quality of my code!

          Comment


          • #6
            And for all of the rest of us reading this years later! Thank you to both Clyde and Rodrigo.

            Comment

            Working...
            X