How to create the name of a macro programmatically and use it as a macro?

93 views Asked by At

I am using the community-contributed command gvselect that permits to perform best subsets variable selection (a statistical method to select variables).

After I used it, I obtain the following result:

. return list

macros:
              r(best3) : " q105capitalisationboursireouvalo q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
              r(best2) : " q12nombredefemmesauconseil q405existenceduncomitdesrmunrati"
              r(best1) : " q405existenceduncomitdesrmunrati"

Then I need to use one of these macros in another statistical estimation method.

For example, I can use the first one as follows:

 xtreg  logremglobale  `r(best3)' i.date, fe

(note the macro `r(best3)' is used as an argument)

My framework entails that I do not know in advance which macro I need to use, so I determine it programmatically.

In this simplified example I may have to use r(best1) , r(best2) or r(best3) (in reality there are more macros available) depending on the data.

All macros generated by gvselect have the same syntax: r(best + number + )

Once I have determined programmatically which macro I have to use (so for instance: number = 1) I'm stuck.

I don't know how to create the name of this macro programmatically and then use it as a normal macro. I'm not even sure if it is possible.

Could you help me with this ?

I have succeeded to replicate the macro name in a normal string as follows:

. scalar inum = 7

. gen macroname =  "r(best" + string(inum,"%8.0g")+")"

. di macroname
r(best7)

But how can I use it in my estimation?

Obviously the following code doesn't work:

xtreg  logremglobale  `macroname' i.date, fe
1

There are 1 answers

1
AudioBubble On BEST ANSWER

The following works for me:

sysuse auto, clear

gvselect <term> weight trunk length, nmodels(2): regress mpg <term> i.foreign

return list

scalars:
            r(nmodels) =  2
                  r(k) =  3

macros:
             r(best31) : " weight trunk length"
             r(best22) : " weight trunk"
             r(best21) : " weight length"
             r(best12) : " length"
             r(best11) : " weight"

matrices:
               r(info) :  5 x 4

scalar inum = 31
local macroname r(best`= inum')

regress price ``macroname''

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(3, 70)        =     12.47
       Model |   221230614         3    73743538   Prob > F        =    0.0000
    Residual |   413834782        70  5911925.46   R-squared       =    0.3484
-------------+----------------------------------   Adj R-squared   =    0.3204
       Total |   635065396        73  8699525.97   Root MSE        =    2431.4

------------------------------------------------------------------------------
       price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |   4.721599   1.132265     4.17   0.000     2.463369    6.979829
       trunk |   28.37644   97.05843     0.29   0.771    -165.2005    221.9534
      length |  -102.6652   42.58687    -2.41   0.019     -187.602   -17.72834
       _cons |   10812.33   4574.211     2.36   0.021     1689.353     19935.3
------------------------------------------------------------------------------