Announcement

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

  • Defining group of variables using 'local' in .do file

    Dear all,

    ​I want to define a group of variables to use in a number of different regressions. The best solution I could come up with is to use 'local' (see example below). This works fine if I run the entire .do file (i.e. the 4 lines in the example below).
    However if I only want to run the 3rd regression, by first running a CTRL-D on the first line (local controls age gender inc) and then a CTRL-D on the last line (logit y3 `controls'), it doesn't work anymore : Stata then just estmates logit y3 (i.e. only constant term) : the controls aren't recognized anymore.

    ----------
    local controls age gender inc
    logit y1 `controls'
    logit y2 `controls'
    logit y3 `controls'
    ----------

    Is there a better way to define a group of variables to be used in different regressions than using 'local'?

    Thanks for your help,
    Mike

  • #2
    Locals are local to the code segment executed, so your use of chunks of the do-file can render them invisible. Globals are the simplest solution here, but they can bite too.

    Comment


    • #3
      Nick is right. The Global option will do exactly what you want, but because it's "more permanent", if you don't restart Stata much, then you may end up with a global that you don't realize is still active in the background. If that's not a concern for you, don't worry about it.

      Comment


      • #4
        Thanks for your clarification regarding codes segments and using only parts of the do-file. That explains why it didn't work
        I changed local to global (see example below), but then it goes wrong from the first equation : only the constant term is estimated

        ----------
        global controls age gender inc
        logit y1 `controls'
        logit y2 `controls'
        logit y3 `controls'
        ----------

        Comment


        • #5
          What's wrong here is that you started but did not finish. You need (e.g.)

          Code:
          global controls age gender inc
          logit y1 $controls

          Comment


          • #6
            Perfect !
            I didn't know the syntax to use global was different from local ($controls vs `controls').

            Thanks a lot.

            Comment


            • #7
              Yes. See [U] 18 for much more. If it were the same syntax, you would need to know about all the globals defined everywhere and anywhere, including in StataCorp's own code, to be sure that a local you define is not using the same name. That is, if `foo' could be a reference to either a global or a local, then the problem of bugs and clashes just explodes.

              Comment


              • #8
                For my part, I avoid globals like the plague because of the dangers that Mike Costello pointed out. (In over 20 years of using Stata I have only one or two do-files that use them.)

                In a situation like Mike Smet raises in the original post, another solution is to "comment out" the regressions that are not wanted and then just run the whole thing:

                Code:
                local controls age gender inc
                *logit y1 `controls'
                *logit y2 `controls'
                logit y3 `controls'
                If large blocks of code need to be skipped over, instead of using the initial *, you can do that with /* */.

                Comment


                • #9
                  I agree with Clyde. Although I have said that globals are the simplest solution, entire years go by without my ever using them of my own accord in Stata. Indeed I can't remember the last time I defined one for myself, unless I wasn't discussing someone else's problem.

                  If you need to segment your do-file repeatedly, you perhaps need shorter do-files.

                  See also include

                  Comment


                  • #10
                    Thanks a lot for your comments.
                    Is there perhaps another way (except using local/global) to define groups of variables that can easily used in a number of regressions?
                    E.g. 10 different dependent variables explained by the same group of independent variables. Changing the composition of the group would allow easy re-estimation without having to change 10 equations.
                    If not, I'm still very happy with the local/global solution : it works. I'll try to remember to regularly macro drop in order to avoid the potential problems mentioned above.

                    Comment


                    • #11
                      You could loop over a set of different response variables (a.k.a. dependent) using foreach. That is another way to use locals in effect.

                      Comment


                      • #12
                        Code:
                        foreach var of varlist dep1 dep 2 dep3 dep4 dep5 {
                             regress `var' indep1 indep2 indep3 indep4
                        }
                        or

                        Code:
                        local independents indep1 indep2 indep3 indep4
                        foreach var of varlist dep1 dep 2 dep3 dep4 dep5 {
                             regress `var' `independents'
                        }

                        Comment


                        • #13
                          Hello all,

                          Assuming one has 80 variables in total. You combine 10 of them using
                          Code:
                          local independents ...
                          as in #12. Is it possible to summarize the remaining 70 in a local without writing out each of the 70 variables? So just to put it bluntly, something like:
                          Code:
                          local remainingvariables 1-`independents'
                          Thank u very much!

                          Comment


                          • #14
                            See

                            Code:
                            help macrolists
                            for tools to manipulate lists.

                            Comment


                            • #15
                              You need to define a local with all the elements first. Then you can do what you want.


                              Code:
                              sysuse auto, clear
                              qui ds
                              local all `r(varlist)'
                              local myset mpg weight trunk displacement
                              local others: list all - myset
                              *Res.:
                              di "`all'"
                              di "`myset'"
                              di "`others'"
                              Res.:

                              Code:
                              . di "`all'"
                              make price mpg rep78 headroom trunk weight length turn displacement gear_ratio foreign
                              
                              . 
                              . di "`myset'"
                              mpg weight trunk displacement
                              
                              . 
                              . di "`others'"
                              make price rep78 headroom length turn gear_ratio foreign

                              Comment

                              Working...
                              X