Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Tutorial: Including Stata Packages

Authors
Affiliations
Cornell University
National Center for Supercomputing Applications
University of Illinois at Urbana-Champaign

Stata packages

Stata users frequently use user-written packages, which are made available to the Stata community via the Stata Journal, SSC, or Github. They are typically installed using a small number of variants of the net install command (including ssc install).

We will first provide guidance on how to install Stata packages into a project, permanently, for the duration of the project until it is turned into a replication package. We then outline an alternate approach, similar to the way that R (CRAN) or Python (PyPi) handle package management.

Embedding Stata packages into a project

Define the environment

An environment will constrain where Stata installs (sysdir PLUS) and seeks (adopath) for ado files.

In your main file, after setting a $rootdir:

/* install any packages locally */
di "=== Redirecting where Stata searches for ado files ==="
capture mkdir "$rootdir/ado"
adopath - PERSONAL
adopath - OLDPLACE
adopath - SITE
sysdir set PLUS     "$rootdir/ado/plus"
sysdir set PERSONAL "$rootdir/ado"       // may be needed for some packages
sysdir                                   // display current settings

All installed packages will now be installed into the project-specific $rootdir/ado, and Stata will look there (and only there) when loading user-contributed packages.

Install packages once

Packages are then installed if not present (f.i., upon first run), but don’t reinstall if already present (on subsequent runs).

*** Add required packages from SSC to this list ***
local ssc_packages ""
    // Example:
    // local ssc_packages "estout boottest"
    //
    display in red "============ Installing packages/commands from SSC ============="
    display in red "== Packages: `ssc_packages'"
    if !missing("`ssc_packages'") {
        foreach pkg in `ssc_packages' {
            capture which `pkg'
            if _rc == 111 {
               dis "Installing `pkg'"
                ssc install `pkg'
            }
            which `pkg'
        }
    }
 ado

Indexing Mata libraries

When Stata packages include Mata libraries, the (separate) Mata index of such files needs to be updated. This is a very quick operation, and never hurts to include it, after all installs.

    mata: mata mlib index

Adding to replication package

The following files should be included in your replication package:

code/ado/*