Using globals and locals from STATA in Python

I’m a big fan of writing STATA dofiles using macros.

For instance, I like the code below because it gives me more control over the y and x variables without changing the command line of the scatter plot.

sysuse auto, clear
local y weight
local x length
twoway scatter `y' `x'
scatter in STATA

I know that’s a straightforward example. But imagine an extended code and one changing its mind about what variable(s) to use every two minutes. Using macros gives me flexibility, and now that I have learned how to use Python within STATA, thanks to this fantastic blog series from Chuck Huber, I want to use the macros I write in STATA in my Python blocks.

Copying macros from STATA to Python

To copy macros from Stata to Python, we use the Macro module from the Stata Function Interface (SFI). To activate it, we import theMacromodule in thesfi library in the Python block.

python: from sfi import Macro

Then, to copy our globals from STATA to Python, we use the Macro.getGlobal() function(for locals use Macro.getLocal())

all together,

sysuse auto, clear
global x length
global y weight
python:
from sfi import Macro
x=Macro.getGlobal("x")
y=Macro.getGlobal("y")

print(x,'and', y)
end

The code above prints the globals content as an output: length and weight.

The magic is that it uses the globals in STATA to print the variables names in Python!

python: 
----------------------------------------------- python (type end to exit) ----------------------------
>>> from sfi import Macro
>>> x=Macro.getGlobal("x")
>>> y=Macro.getGlobal("y")
>>> print(x,'and', y)
length and weight
>>> end

An expanding world of macro opportunities.

Let’s reproduce the same plot we did initially but this time using Python.

Instead of importing just the words, we can import each var into Python using the functionData.get().

And then write a Python block to plot the scatter.

sysuse auto, clear
global y weight
global x length
python:
from sfi import Data
x=Data.get(Macro.getGlobal("x"))
y=Data.get(Macro.getGlobal("y"))

from matplotlib import pyplot as plt
plt.scatter(x, y)
plt.xlabel(Macro.getGlobal("x"))
plt.ylabel(Macro.getGlobal("y"))
plt.show()
end.

Et Voilà. The same plot but in Python.

scatter in Python

Notice how I use the variable names in my dofile only ONCE! And how I import the macros to write the Python code.

I even label the axes importing the globals from STATA, not manually. Because if I ever want to change the variables, I don’t want to re-write the axes labels again.

If we feel like changing the variables, we just update our STATA globals at the top of our code.

sysuse auto, clear
global y trunk
global x mpg
twoway scatter $y $xpython:
from sfi import Data
x=Data.get(Macro.getGlobal("x"))
y=Data.get(Macro.getGlobal("y"))

from matplotlib import pyplot as plt
plt.scatter(x, y)
plt.xlabel(Macro.getGlobal("x"))
plt.ylabel(Macro.getGlobal("y"))
plt.show()
end

And we get the updated plot.

--

--

--

Un hijo más de los 17 que tuvo el coronel Aureliano Buendía durante las 32 guerras civiles que luchó.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Install EMQ (emqtt) 2.3 on Linux

Introducing Cloudflare Workers: Run JavaScript Service Workers at the Edge

Microsoft Flow Send Email from HTTP Request

A Closer Look Into AMD’s GCN Architecture

4 Podman Options, one must know!!!

How to use sprites in Unity?

Converting a texture into a sprite

Dora Factory x Im Community AMA Recap

Getting started with Kotlin Multiplatform Mobile (KMM)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Oscar Díaz

Oscar Díaz

Un hijo más de los 17 que tuvo el coronel Aureliano Buendía durante las 32 guerras civiles que luchó.

More from Medium

How to plot a stacked bar chart with custom labels and legend with Matplotlib

Pandas — SettingwithCopy Warning and the .loc Function

Python Paint Color Selector

Understanding P-values and How to Interpret Them