Announcement

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

  • Save summaries of variables in macros

    Hello,

    I'm wondering if there is a way to save descriptive statistics in a local macro, in one line. I could use sum, detail and then use r(min) r(max), etc, or I could use egen var = min(var2) etc, but I would prefer to do this in one line if possible, because I don't need these variables, and I think keeping track of many tempvars will get messy.

    Here's an example of what I'd like to do. Note that this won't run, it's just an example.

    Code:
    file write report "At Six Months, `=mean(incf6) if rain == 1', `=min(incf6) if rain ==1 & incf6 > 0' "
    Or, something like:

    Code:
    loc minvar = min(var)
    Any ideas? Thanks!

  • #2
    Look at

    Code:
    help summarize
    where part of what you want to know is documented, namely what is available immediately after a call to that command. The rest is documented in
    http://www.stata.com/manuals14/u18.pdf (see 18.8) as what you can do with such results.

    Your example would seem to call for

    Code:
    su incf6 if rain == 1, meanonly
    local mean = r(mean)
    su incf6 if rain ==1 & incf6 > 0
    file write report "At Six Months, `mean', `r(min)'"
    which illustrates the two main ideas:

    1. Immediately after an r-class (or e-class) command, you can access r-class (or in other cases e-class) results directly.

    2. Otherwise you must save such results somewhere for later use, e.g. in a local macro or scalar.

    It's often overlooked that many key results are available after summarize, meanonly

    I have said nothing here about pushing results through a suitable display format.


    See also http://www.stata-journal.com/sjpdf.h...iclenum=pr0053 for a review of basic techniques (or, if you like, a quiz on what you should know: what is your score?)

    Comment


    • #3
      Thanks Nick Cox , that is essentially what I have been using up to now (sans meanonly). I was hoping for a way to access summaries directly in-line (without having to specify summarize first), but it isn't a big difference. Thank you for this!

      Comment


      • #4
        I got the impression that you were dismissing that, but it's the best way to do this unless you want to automate everywhere in Mata.

        Comment

        Working...
        X