Multiple(ish) Stata Instances on Mac
by Shawna K. Metzger
shawnakmetzger.com/wp/stata-mac/
Accessed November 21, 2024. Last modified September 8, 2020
Warning: ‘Multiple(ish)’
Before you get your hopes up: this won’t allow you to work live in multiple Stata instances simultaneously on Mac.
FYI: Offered As-Is
I can’t help troubleshoot specific problems you might run into because I don’t typically have access to a Mac. Most issues arise from getting RStata
set up correctly. The package’s documentation is your best bet.
Overview
If you’re a Mac owner and you have lots of Stata code you need to run in the background (cleaning a huge dataset, say, or running a large set of Monte Carlo simulations), this is the post for you. We’ll be exploiting Stata’s ability to run in batch mode (running behind the scenes without any direct user input) to have multiple Stata windows open in the background.1 You can still have a single Stata window open in the foreground that you’re working in while the background jobs run.
There are a few ways to run Stata in batch mode on Mac. All of the official ways involve Terminal…
…but because the universe loves irony, we can also use R to run Stata in batch mode, which may be a less intimidating prospect (even if you’ve never used R before in your life) because regular Stata syntax makes an appearance. Unlike Stata, you can run multiple R sessions on Mac at the same time, giving you the ability to run multiple Stata batch jobs.
Setup
The instructions below generalize to everyone, but are written from the perspective of Stata users who deal with R infrequently to not at all.
> Projected setup time: 5–15 minutes (depending on download speeds)
- Install R, if it isn’t already installed (here)
- Open R
- Install the
RStata
package, which gives us the ability to call Stata from R. To do this, type install.packages("RStata") into the R Console window. You’ll only have to do this once per R installation, same as how you have to install user-written Stata commands once per Stata installation. - Unlike Stata, you have to tell R which packages you’ll be using every time you open a new R session. To tell R “I’m going to be using commands from the
RStata
package,” type library("RStata"). RStata
needs to know where your Stata installation’s located on your computer. You set this information usingoptions()
in R. The info won’t persist across R sessions, so you’ll have to declare it every time you open R and want to run a Stata batch job.2To set these options, figure out where your computer’s Stata installation is and what Stata version you have. Then, in R, type (# = comments, don’t need to include):
12options("RStata.StataPath"="/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp") # If this is where your Stata installation's locatedoptions("RStata.StataVersion"=16) # If you have version 16Note that the path incorporates your Stata flavor. In the example above, the local Stata installation is MP. If you have Stata SE, the above path would change to something like options("RStata.StataPath"="/Applications/Stata/StataSE.app/Contents/MacOS/stata-se").
The
Contents/MacOS/stata-se
part is a must, because R needs the “raw” location of the Stata installation, not the location of Stata’s GUI. (The GUI’s in/Applications/Stata/StataSE.app
, in our example.) If you run into trouble,RStata
’s author has more to say about finding the proper installation path on Mac here.- R should now be ready to accept Stata commands. You’ll need to pass them as a string to
RStata
’sstata()
function. If everything’s set up the way it should be, typing stata('display "Look, it works!"') should echo back “Look, it works!” to the R window after a second or two.3Notice that the contents of
stata()
are valid lines of Stata code. R will take what you enter, strip off the outer''
s, and pass the rest to Stata to evaluate. Ergo, all of Stata’s syntax rules still apply forstata()
’s contents.As an example of these syntax rules in action: R is fine with
''
and""
being used interchangeably to define a string, but Stata isn’t. It’s important that, forstata()
’s contents, the outer quotes be''
and the inners be""
.4 You’ll notice the outer''
+ inner""
pattern carries through to the next section’s examples. RStata
works by calling Stata in batch mode. Therefore, if the previous step worked for you, you now have the ability to run multiple Stata instances on Mac via R.
How about an actual example?
Absolute Paths
Say we had a do-file named testRun with spaces in name.do
, and that this file’s saved to our main Dropbox folder (assuming the default installation options for Dropbox). The do-file’s contents are:
1 2 3 4 |
forvalues i=1/20{ display as green "We're on number " as yellow `i' as green " of 20." sleep 2000 // wait 2 seconds } |
(The display
colors won’t print in R’s window, but they’re there simply to reinforce that there’s nothing special about the do-file’s formatting. It’s the same old do-file you’d run in Stata.)
We want to run this do-file from R. To do it, we would open R, then type:
1 2 3 4 5 6 7 8 9 |
# Load the RStata package (# = R's comments, equivalent to // in Stata) library(RStata) # Set the options, so R knows where Stata's located (continue using previous fake path/vers) options("RStata.StataPath"="/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp") options("RStata.StataVersion"=16) # Run the do-file stata('do "~/Dropbox/testRun with spaces in name.do"') |
If you run this code in R, then try to open up another Stata instance, you’ll be able to do so without a problem.
If you had a second do-file to also run in the background, you would need to open a second R session and repeat this process, with the second do-file’s name inserted. (The first R session will be busy until the first do-file finishes running.)
To repeat an earlier point: notice that the outer quotes for stata()
’s input string are single ''
s, but any inner quotes are double ""
s, consistent with Stata’s syntax conventions.
Setting Directory
If you didn’t want to type the absolute path to the do-file, you can set R’s working directory to be ~/Dropbox
. You’d do this with cd
in Stata; R’s equivalent is setwd()
. You’d change the previous code chunk to read:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Load the RStata package (# = R's comments, equivalent to // in Stata) library(RStata) # Set the options, so R knows where Stata's located (continue using previous fake path/vers) options("RStata.StataPath"="/Applications/Stata/StataMP.app/Contents/MacOS/stata-mp") options("RStata.StataVersion"=16) # Set the working directory setwd("~/Dropbox") # Run the do-file stata('do "testRun with spaces in name.do"') |
R’s Do-File Equivalent
R’s do-file equivalent is known as an R file. They’re text files with a .R
extension. To run an R-file in R, you’d type
source('theFilePath/FileNameHere.R'), where you’d insert either the absolute path to the R-file’s location or setwd()
to its location, then just enter its name.
To save time, then, you could copy all of example 1 or example 1a’s code into a text file, save it with a .R
extension, then run that file in R, saving you from having to type or copy/paste the lines more than once.
If you’ve ever submitted a Stata job to run on a supercomputer/HPC cluster, that’s what we’ll be doing on your computer—submitting “jobs” for Stata to run without being able to interact with the job’s Stata session. ↩
Alternatively, you can also insert these two lines of code into
.Rprofile
orRprofile.site
, if you know what those files are. They auto-load when R begins, meaning you don’t have to type anything additional once you’ve inserted the code into one of those two files. ↩To access the command’s R help file, type
?stata
. ↩You could also use doubles for everything, but would have to escape the second set:
stata("do \"~/Dropbox/testRun with spaces in name.do\"")
. If this makes more sense to you, go for it. ↩