Stata notebooks
Overview
Stata notebooks are available for those researchers who are more comfortable using Stata and its ecosystem. These are built off the same base image as python notebooks, but include the official pystata library to allow for the execution of Stata in a notebook environment.
Working with Stata in a notebook environment is slightly different than the Stata desktop application, in that you need to utilize python to pass data into Stata. This step is quite simple, and doesn't require any expertise in python. See working with tabular data below.
Enabling Stata notebooks
Because Stata is proprietary software, you must provide a license for Stata 16 or later in order to enable Stata notebooks on Redivis. Organizations can specify license information in their settings, which will make Stata notebooks available to all members of their organization. Alternatively, you can provide your own stata license in your workspace.
Base image and dependencies
Stata notebooks are based off the python notebook base image, and can combine both Stata and Python dependencies to create novel workflows.
To further customize your compute environment, you can specify various dependencies by selecting Dependencies at the top-right of a notebook. Here you will see three tabs: Packages, pre_install.sh, and post_install.sh.
Use packages to specify the python packages that you would like to install. When adding a new package, it will be pinned to the latest version of that package, but you can specify another version if preferred.
In order to install Stata packages via ssc, you should use the pre- and post- install shell scripts. These scripts are executed on either side of the python package installation, and are used to execute arbitrary code in the shell. Here you can execute stata code to run ssc install, and you can also use apt to install system packages (apt-get update && apt-get install -y <package>), or mamba to install from conda. E.g.
# A shell script. You can run Stata code here,
# or install other system dependencies via apt-get
stata -e -q 'ssc install outreg2'Working with tabular data
In order to load data into Stata, use a one-liner to pull any Redivis table or SQL query result, into Stata. All variable metadata, including value labels, will be loaded with the data. This will overwrite any working dataset that we currently have in your Stata session.
Next, in a separate cell, use the %%stata "magic" at the start of your cell to specify that this is stata code.
Any subsequent cells that execute stata code should be prefixed by %%stata if they are more than one line, or by %stata if the code to be executed is all on one line:
You can also use the %%mata command to execute Mata code:
Working with geospatial data
Loading geospatial data looks much the same as other tabular data, in that you can just call the .to_stata() method on any table or query in python. If that table or query result contains a geography variable, it is automatically loaded as geospatial data to Stata via the spshape2dta package.
In the uncommon case where your table has multiple variables of type geography, you'll need to explicitly specify which variable to use as the geography.
If you want to bypass the default behavior and load a table with a geography variable as a standard Stata table, you can explicitly set .to_stata(geography_variable = None).
Creating output tables
Redivis notebooks offer the ability to materialize notebook outputs as a new table node in a workflow. This table can then be processed by transforms, read into other notebooks, exported, or even re-imported into a dataset.
To create an output table, you must first save your Stata data set to a .dta file. You can then use the redivis.current_notebook().create_output_table() method in python to output your data.
If an output table for the notebook already exists, it will be overwritten by default . You can pass append=True to append, rather than overwrite, the table. In order for the append to succeed, all variables in the appended table that are also present in the existing table, must have the same type.
Storing files
As you perform your analysis, you may generate files and figures that are stored on the notebook's hard disk. There are two locations that you should write files to: /out for persistent storage, and /scratch for temporary storage. By default, the output location is set to /scratch.
Any files written to persistent storage will be available when the notebook is stopped, and will be restored to the same state when the notebook is run again. Alternatively, any files written to temporary storage will only exist for the duration of the current notebook session.
Last updated
Was this helpful?

