Macro (sfi.Macro)

class sfi.Macro

This class provides access to Stata macros.

Method Summary

getGlobal(name) Get the contents of a global macro.
getLocal(name) Get the contents of a local macro.
setGlobal(name, value[, vtype]) Set the value of a global macro.
setLocal(name, value) Set the value of a local macro.

Method Detail

static getGlobal(name)

Get the contents of a global macro.

Parameters:name (str) –

Name of the macro. It can be one of the following:

  • global macro such as “myname”
  • return() macro such as “return(retval)”
  • r() macro such as “r(names)”
  • e() macro such as “e(cmd)”
  • c() macro such as “c(current_date)”
  • s() macro such as “s(vars)”
Returns:Value of the macro. Returns an empty string if the macro is not found.
Return type:str
static getLocal(name)

Get the contents of a local macro.

Parameters:name (str) – Name of the macro.
Returns:Value of the macro. Returns an empty string if the macro is not found.
Return type:str
static setGlobal(name, value, vtype='visible')

Set the value of a global macro. If necessary, the macro will be created.

Parameters:
  • name (str) –

    Name of the macro. It can be one of the following:

    • global macro such as “myname”
    • return() macro such as “return(retval)”
    • r() macro such as “r(names)”
    • e() macro such as “e(cmd)”
    • s() macro such as “s(vars)”
  • value (str) – Value to store in the macro.
  • vtype ({'visible', 'hidden', 'historical'}, optional) – If the macro is a type of return value, vtype sets whether the return value is visible, hidden, or historical. This parameter is ignored when the macro is a global or a s() macro. Default is ‘visible’.
static setLocal(name, value)

Set the value of a local macro. If necessary, the macro will be created.

Parameters:
  • name (str) – Name of the macro.
  • value (str) – Value to store in the macro.

Examples

The following provides a few quick examples illustrating how to use this class:

>>> from sfi import Macro
>>> stata: sysuse auto, clear
(1978 Automobile Data)
>>> stata: regress mpg weight foreign

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(2, 71)        =     69.75
       Model |   1619.2877         2  809.643849   Prob > F        =    0.0000
    Residual |  824.171761        71   11.608053   R-squared       =    0.6627
-------------+----------------------------------   Adj R-squared   =    0.6532
       Total |  2443.45946        73  33.4720474   Root MSE        =    3.4071

------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175
     foreign |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422
       _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768
------------------------------------------------------------------------------
>>> stata: ereturn list

scalars:
                  e(N) =  74
               e(df_m) =  2
               e(df_r) =  71
                  e(F) =  69.74846262000308
                 e(r2) =  .6627029116028815
               e(rmse) =  3.407059285651584
                e(mss) =  1619.287698167387
                e(rss) =  824.1717612920727
               e(r2_a) =  .6532015851691599
                 e(ll) =  -194.1830643938065
               e(ll_0) =  -234.3943376482347
               e(rank) =  3

macros:
            e(cmdline) : "regress mpg weight foreign"
              e(title) : "Linear regression"
          e(marginsok) : "XB default"
                e(vce) : "ols"
             e(depvar) : "mpg"
                e(cmd) : "regress"
         e(properties) : "b V"
            e(predict) : "regres_p"
              e(model) : "ols"
          e(estat_cmd) : "regress_estat"

matrices:
                  e(b) :  1 x 3
                  e(V) :  3 x 3

functions:
             e(sample)

>>> Macro.getGlobal('e(cmdline)')
'regress mpg weight foreign'
>>> Macro.setGlobal('e(cmdline)', 'Cmd: regress mpg weight foreign')
>>> Macro.getGlobal('e(cmdline)')
'Cmd: regress mpg weight foreign'
>>> Macro.setLocal('lm', 'This is a local macro')
>>> Macro.getLocal('lm')
'This is a local macro'