Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: How to find a macro containing a given string

Subject   Re: st: How to find a macro containing a given string
Date   Tue, 23 Aug 2005 10:27:59 -0500

Joseph Coveney <> asks:

> I'd like to be able to find the name of a macro that contains a given
> string.  After -anova-, there is a list of ereturn macros that contain the
> name of the term in the model and the number assigned to the term
> by -anova-.  I want to be able to do arithmetic on the scalars (sum squares
> and degrees of freedom) associated with that term's number by referencing
> the model's term.
> Example:  Let's say -ereturn list- after -anova- shows
> . . .
> e(term_1) : "clinic"
> e(term_2) : "physician|clinic"
> . . .
> I would like to write a program with a syntax of, say,
> -satterthwaite clinic physician|clinic . . .-
> and have the program query -anova-'s ereturn macros in order to get the
> names of the macros corresponding to these terms.  I can then parse the
> numerals from the macro names in order to look up the corresponding ereturn
> scalars, e(ss_1), e(df_1), e(ss_2), e(df_2), . . ., and do arithmetic on
> them.
> I've scanned through the ado-file for -test- (perhaps it's in -_test-), and
> looked at -help macros-, and its linked help files, but nothing jumped out
> at me.  I can always use brute force to go through the list of ereturn
> macros in a loop, or concatenate their contents in-sequence (again, a loop) 
> and use -posof-.  But is there anything more direct?

I would use a loop.  I think it is rare to have thousands of
terms in an ANOVA, so a loop should not be overly time consuming.

    local i 1
    local index 0
    while e(term_`i') != "" {
            if e(term_`i') == "`whatever'" {
                    local index `i'
                    continue, break
            local ++i
    if !`index' {
            di as err "`whatever' not found in most recent anova"
            exit 147

    <... code that refers to e(df_`index') and e(ss_`index') ...>

Obviously replace `whatever' with whatever is appropriate for
your program.  The -continue, break- inside the loop pops you out
of the loop as soon as the term is found (thus saving execution
time if the term is early in the list).

Ken Higbee
StataCorp     1-800-STATAPC

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index