Announcement

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

  • Short question: Local command doesn't work in dofile just command window - why?

    Dear users,

    I have issues with the local command. The command doesn't work when I type it in my dofile just when I write it directly in the command window. Do you knwo why and how I can change that?

    One example Code:

    regress Y X if abs(X)<=10 & Z
    local a1 = _b[_cons]

    When I run it from my dofile and type
    display `a1' - nothing shows. But when I run it from the command window in stata I get the right result..??

    Thanks and regards
    Franziska

  • #2
    Hi Franziska
    What you are finding there is exactly the nature of a -local-. They only "exist" in memory for the instance that they were created. In this case, since they are created within a dofile, that local will no longer be there when trying to retrieve it from the command window.
    But, if you create a local in the command window, you will be able to, since they "exist" in that session.
    I find that locals are very useful when creating codes and temporary lists, vars files, etc. However, if you want to use the information later, you may want to use a -global- instead
    Fernando

    Comment


    • #3
      The local command in your do-file worked. It just did't do what you expect.

      The point of a local macro is that it is visible only within the space in which it is created. That's what "local" means.

      "Space" means each separate

      program

      or

      do-file

      or

      (chunk of) do-file editor contents

      or

      interactive session.

      If you want something in your do-file to be visible outside you must create it as something else, say a scalar.

      More could be said at a more advanced programming level.

      Comment


      • #4
        Thanks for your help!

        Comment

        Working...
        X