Announcement

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

  • The use of the symbol $ in Stata commands

    Hi Statalist

    In many of the questions posted, the following syntax has been used: e.g. $covariates or $xlist. Can I please ask 1) what exactly does the $ symbol do in a Stata command and 2) how does one go about creating something like $xlist?

    I have tried - help $ - and googling search terms like "Stata what does $ mean" but haven't yet found the answers.

    Thanks.

  • #2
    It references a global macro in such examples. https://www.stata.com/manuals/u18.pdf is for serious Stata users.

    Comment


    • #3
      I see, many thanks Nick. I guess I still have some way to go in my Stata learning before I start using these macros, but it's very helpful to at least know what they are now when I read other people's codes. Thanks.

      Comment


      • #4
        I will chime in here and point out that the use of global macros for the purpose of storing lists of variables or other things is almost always poor programming practice and should be avoided. Global macros are, well, global. If you are running any other program that uses a global macro of the same name, you will clobber it, or it will clobber you. The use of local macros is a much safer practice, as they have meaning only within the do-file, highlighted block of code within a do-file, program, or Command window session within which they are defined, so they cannot clash with things that you may not even be aware exist. These limitations of local macros makes them safer to use: they cannot be altered without your knowledge and they will not interfere with background programs you may or may not realize are running. These limitations also make them somewhat inconvenient to use in some circumstances--but there are simple enough workarounds for these situations and you should resist the temptation to use global macros. You don't often get in trouble with them, but when you do, the resulting bugs are incredibly difficult to find and fix. If it ever happens to you once, you will never want to have it happen to you again.

        When you get around to learning to use macros, develop the good habit of using local macros unless there is no alternative to using a global. Such situations are rare. I am just shy of my 25th anniversary as a Stata user, and in all that time, I have only once found it necessary to resort to a global macro.

        Comment


        • #5
          Thank you very much Clyde for your advice. I will keep in mind what you said about the pitfalls of using global macros and that it is, in the majority of cases, much more advisable to use local macros instead. Thanks again.

          Comment

          Working...
          X