Announcement

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

  • Saving commands as macro variables (local and global) to make do-files shorter

    Hi.

    I have a few lines of commands which are repeated after each regression. They save the details of the regression for further tabulating of the results. I have a lot of regressions and in many cases the post estimate commands are exactly the same. Now I wonder if there is a way to save these lines as macro (local/global) variables and just use the name of the variables to call the commands. I know how to save the commands in a separate do-file and call the do-file in the main program, but I'd rather not to have separate do-files just for a few lines of commands.

    Example:

    logit .....

    eststo reg`i'
    estadd local ind_fe "Yes"
    estadd local municip_fe "Yes"
    estadd local year_fe "Yes"
    estadd local ind_year_fe "No"

    ....

    (These are the lines to be added to the footnote of the regression tables regarding the dummies and fixed effects included)

    Thanks in advance,
    Fatima

  • #2
    You can save commands in macros.

    Code:
    local command1 "sysuse auto, clear"
    local command2 "regress mpg weight"
    `command1'
    `command2'
    I don't see an advantage in doing this compared to storing the commands in a separate do-file that you then call with a single include command.

    Comment


    • #3
      This is more subtle than it appears, because the first of Fatima's commands includes a reference to the macro i and presumably there will be different values of i for each regression. The suggestion of using the include command is helpful, because unlike files called with the do command, when a file is included, it can refer to local variables that were defined in the do-file into which it is included.

      Another possiblity would be to define a simple program within the do-file. For example, near the start of the do-file we define the footnote program, which will take one argument.
      Code:
      capture program drop footnote
      program define footnote
      eststo `1'
      estadd local ind_fe "Yes"
      estadd local municip_fe "Yes"
      estadd local year_fe "Yes"
      estadd local ind_year_fe "No"
      end
      Then further down in the do-file we call the footnote program after running the logit command.
      Code:
      logit .....
      
      footnote reg`i'
      For further information on Stata's program command, see the description in the Stata Programming Reference Manual.

      Comment

      Working...
      X