Announcement

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

  • Replace extensive, repeated code (as in table settings) with a placeholder to change it at once for all tables in the document, instead of changing every instance

    Having a long .do file with many graphs and tables being exported, I face the problem that when changing some settings, I need to work manually through the whole .do-file to change the settings of every graph by hand. At best, I can use the "replace" function (not in Stata as far as I know, but when opening the file in notepad). I am looking for a way to work more efficiently here.

    specifically, an often repeated chunk of code in my work is:

    Code:
    sysuse auto.dta
        histogram mpg, start(0) frequency color(gs12) graphregion(color(white)) ///
        graph export mpg.png, replace
    This is repeated multiple times with different variables than mpg.

    Now, one option for me would be to define the graph settings centrally for all histograms and other graphs. But this would make me unflexible if I need a few graphs with different settings, e.g. different colorstyle.

    What I am looking for is a rather crude way of defining a placeholder such as "mysettings" in the beginning of the .do-file, such as:

    Code:
    mysettings = "frequency color(gs12) graphregion(color(white))",
    then using only

    Code:
     histogram mpg, mysettings ///
        graph export mpg.png, replace
    Similarly, my esttab command requires quite heavy modification before I output it into latex, for which I use addnotes as follows:

    Code:
    esttab est1 est2 ///
        using regcompare1.tex, mtitles ///
        p r2 replace noconstant ///
        nonotes addnotes("\textit\(p\)-values in parentheses" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{*}} \(p<\) 0.05" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{**}} \(p<\) 0.01" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{***}} \(p<\) 0.001")
    After having figured this out, replacing it in my dozens of tables was a pain, which would have been relieved by

    Code:
    "mytabsettings" = "p r2 replace noconstant ///
        nonotes addnotes("\textit\(p\)-values in parentheses" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{*}} \(p<\) 0.05" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{**}} \(p<\) 0.01" ///
        "\makebox[\widthof{\sym{***}}][r]{\sym{***}} \(p<\) 0.001")"
    and then

    Code:
    esttab est1 est2 ///
        using regcompare1.tex, mtitles ///
        mytabsettings
    Ideally, I am not looking for centralized options, but just placeholders that I can define at one place and that do not interfere at all with the actual table creation, but only with my comfort in handling the document.
    Last edited by Max Piper; 07 Jan 2016, 05:27.

  • #2
    Stata's do-file editor has a "replace".

    More generally, one answer is loops.

    Code:
      
    sysuse auto.dta
    foreach v of var rep78-foreign {      
        histogram `v', start(0) frequency color(gs12) graphregion(color(white))
        graph export `v'.png, replace
    }
    Another answer is local macros. You can define a local macro

    Code:
      
    local mysettings  frequency color(gs12) graphregion(color(white))
    and then refer to that throughout the rest of a do-file. You can also re-define it. This could be used for your more complicated problem, as it's just the same idea.

    Browsing in the [U] manual in strongly recommended. See also e.g. (while noting that for is utterly superseded now by foreach and forvalues)


    http://www.stata-journal.com/article...article=pr0003

    http://www.stata-journal.com/article...article=pr0005

    Last edited by Nick Cox; 07 Jan 2016, 04:55.

    Comment


    • #3
      Code:
      // open example dataset
      sysuse auto.dta
      
      // store my settings
      local mysettings  "frequency color(gs12) graphregion(color(white))"
      
      // get a list of all numeric variables in memory
      ds, has(type numeric)
      local varl `r(varlist)'
      
      // create the historgram with my settings for all numeric variables
      foreach var of local varl {
          histogram `var', `mysettings' name(`var', replace)
      }
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thank you both for these helpful answers. The macro idea is particularly helpful.

        Comment


        • #5
          The brewscheme package was created exactly to prevent this from happening (at least in the case of graphs). If you have a .scheme file that has all of the aesthetic properties you want included then you can pass the name of the scheme to the scheme option of a command and all of those aesthetic choices get implemented. Creating a scheme file by hand can be a rather laborious process, but can be reduced to a few seconds (at most) using brewscheme.

          Comment

          Working...
          X