Announcement

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

  • Using tabstat with macro?

    I am using BWGHT.DTA with a macro created as:

    local bwghtvars bwght cigs faminc fatheduc motheduc parity male white

    I thought I could use tabstat as follows:

    tabstat `bwghtvars`

    Unfortunately, it throws an error:

    . tabstat `bwghtvars'
    varlist required
    r(100);

    Isn't the macro a variable list?






  • #2
    I don't know what BWGHT.DTA is--it isn't a built-in or Stata -webuse- file. Anyway, what you are doing looks correct to me, and an analogous code works fine for me:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    .
    . local my_varlist price mpg headroom gear_ratio
    
    .
    . tabstat `my_varlist'
    
       stats |     price       mpg  headroom  gear_r~o
    ---------+----------------------------------------
        mean |  6165.257   21.2973  2.993243  3.014865
    --------------------------------------------------
    My guess is that you are running your code one line at a time instead of all in one fell swoop. You can't do that with local macros. When you try to do that, local macro bwghtvars will go out of scope (i.e. cease to exist) as soon as you create it, so when you then try your -tabstat- command, Stata sees no variables listed. But if you run the commands all together, the local macro persists.

    Whenever you run a single line or a block of lines together, any local macros created go out of existence at the end of that block. So if you create a local macro in one place and use it in another, you have to run all of the code from the creation to the use together at one time.

    Comment


    • #3
      I see. You're correct. I was running one line at a time. It does work when run together. Is there a way to get it to persist while I am working on a .DO file and running one line at a time?

      Comment


      • #4
        No, it is the very definition of a local macro that its scope ends with the block of code that created it.l

        If the situation is that in your real do-file there is a lot of code between the definition of the macro and the -tabstat- command, and you don't want to keep re-executing all of that (and assuming that the intervening code doesn't change the definition of the macro) you can just comment out the intervening code until you're done with your experimentation.

        Comment

        Working...
        X