- Creating virtual environments in Stata is feasible
- Doing so stabilizes the code, and makes it more transportable
In Stata, we typically do not talk about environments, but the same basic structure applies: Stata searches along a set order for its commands.
Some commands are built into the executable (the software that is opened when you click on the Stata icon), but most other internal, and all external commands, are found in a search path.
:::: {.columns}
::: {.column width=40%}
The default set of directories which can be searched, from a freshly installed Stata, can be queried with the sysdir
command, and will look something like this:
sysdir
:::
::: {.column width=60%}
STATA: C:\Program Files\Stata18\
BASE: C:\Program Files\Stata18\ado\base\
SITE: C:\Program Files\Stata18\ado\site\
PLUS: C:\Users\lv39\ado\plus\
PERSONAL: C:\Users\lv39\ado\personal\
OLDPLACE: c:\ado\
:::
::::
:::: {.columns}
::: {.column width=40%}
The search paths where Stata looks for commands is queried by adopath
, and looks similar, but now has an order assigned to each entry:
adopath
:::
::: {.column width=60%}
[1] (BASE) "C:\Program Files\Stata18\ado\base/"
[2] (SITE) "C:\Program Files\Stata18\ado\site/"
[3] "."
[4] (PERSONAL) "C:\Users\lv39\ado\personal/"
[5] (PLUS) "C:\Users\lv39\ado\plus/"
[6] (OLDPLACE) "c:\ado/"
:::
::::
:::: {.columns}
::: {.column width=40%}
To look for a command, Stata will look in the first directory, then the second, and so on, until it finds it. If it does not find it, it will return an error.
which reghdfe
:::
::: {.column width=60%}
command reghdfe not found as either built-in or ado-file
r(111);
:::
::::
:::: {.columns}
::: {.column width=40%}
When we install a package (net install
, ssc install
)1, only one of the (sysdir
) paths is relevant: PLUS
.
:::
::: {.column width=60%}
[1] (BASE) "C:\Program Files\Stata18\ado\base/"
[2] (SITE) "C:\Program Files\Stata18\ado\site/"
[3] "."
[4] (PERSONAL) "C:\Users\lv39\ado\personal/"
[5] (PLUS) "C:\Users\lv39\ado\plus/"
[6] (OLDPLACE) "c:\ado/"
:::
::::
:::: {.columns}
::: {.column width=40%}
ssc install reghdfe
which reghdfe
:::
::: {.column width=60%}
. ssc install reghdfe
checking reghdfe consistency and verifying not already installed...
installing into C:\Users\lv39\ado\plus\...
installation complete.
. which reghdfe
C:\Users\lv39\ado\plus\r\reghdfe.ado
*! version 6.12.3 08aug2023
:::
::::
:::: {.columns}
::: {.column width=40%}
But the (PLUS)
directory can be manipulated
:::
::: {.column width=60%}
* Set the root directory
global rootdir : pwd
* Define a location where we will hold all packages in THIS project (the "environment")
global adodir "$rootdir/ado"
* make sure it exists, if not create it.
cap mkdir "$adodir"
* Now let's simplify the adopath
* - remove the OLDPLACE and PERSONAL paths
* - NEVER REMOVE THE SYSTEM-WIDE PATHS - bad things will happen!
adopath - OLDPLACE
adopath - PERSONAL
* modify the PLUS path to point to our new location, and move it up in the order
sysdir set PLUS "$adodir"
adopath ++ PLUS
* verify the path
adopath
:::
::::
:::: {.columns}
::: {.column width=40%}
* Set the root directory
global rootdir : pwd
* Define a location where we will hold all packages in THIS project (the "environment")
global adodir "$rootdir/ado"
* make sure it exists, if not create it.
cap mkdir "$adodir"
* Now let's simplify the adopath
* - remove the OLDPLACE and PERSONAL paths
* - NEVER REMOVE THE SYSTEM-WIDE PATHS - bad things will happen!
adopath - OLDPLACE
adopath - PERSONAL
* modify the PLUS path to point to our new location, and move it up in the order
sysdir set PLUS "$adodir"
adopath ++ PLUS
* verify the path
adopath
:::
::: {.column width=60%}
. adopath
[1] (PLUS) "C:\Users\lv39\Documents/PROJECT123/ado/"
[2] (BASE) "C:\Program Files\Stata18\ado\base/"
[3] (SITE) "C:\Program Files\Stata18\ado\site/"
[4] "."
::: ::::
:::: {.columns}
::: {.column width=40%}
Let's verify again where the reghdfe
package is:
which reghdfe
:::
::: {.column width=60%}
. which reghdfe
command reghdfe not found as either built-in or ado-file
r(111);
:::
::::
So it is no longer found. Why? Because we have removed the previous location (the old PLUS
path) from the search sequence. It's as if it didn't exist.
:::: {.columns}
::: {.column width=50%}
Previously:
. which reghdfe
C:\Users\lv39\ado\plus\r\reghdfe.ado
*! version 6.12.3 08aug2023
:::
::: {.column width=50%}
. adopath
[1] (PLUS) "C:\Users\lv39\Documents/PROJECT123/ado/"
[2] (BASE) "C:\Program Files\Stata18\ado\base/"
[3] (SITE) "C:\Program Files\Stata18\ado\site/"
[4] "."
:::
::::
When we now install reghdfe
again:
. ssc install reghdfe
checking reghdfe consistency and verifying not already installed...
installing into C:\Users\lv39\Documents\PROJECT123\ado\plus\...
installation complete.
. which reghdfe
C:\Users\lv39\Documents\PROJECT123\ado\plus\r\reghdfe.ado
*! version 6.12.3 08aug2023
We now see it in the project-specific directory, which we can distribute with the whole project.
Let's imagine we need an older version of reghdfe
.
- In general, it is not possible in Stata to install an older version of a package in a straightforward fashion.
- You may have success with the Wayback Machine archive of SSC.
Most package repositories are versioned:
- R: CRAN, Bioconductor
- Python: PyPI
- Julia: "General" default Julia package registry.
Stata does not (as of 2024). But see the full site for one approach.
From the earlier desiderata of environments:
- ✅ Isolated: Installing a new or updated package for one project won't break your other projects, and vice versa.
- ✅ Portable: Easily transport your projects from one computer to another, even across different platforms.
- ❌ Reproducible: Records the exact package versions you depend on, and ensures those exact versions are the ones that get installed wherever you go.
- your code runs without problem, after all the debugging.
- your code runs without manual intervention, and with low effort
- it actually produces all the outputs
- your code generates a log file that you can inspect, and that you could share with others.
- ❓ it will run on somebody else's computer
Footnotes
-
net install
refererence. Strictly speaking, the location where ado packages are installed can be changed via thenet set ado
command, but this is rarely done in practice, and we won't do it here. ↩