StataCpp

Here are a few tips on mixing C++ with Stata (partly for my future reference). The tips also apply to other languages.

The official plugin interface

This is described at http://www.stata.com/plugins/. Download the header file and skeleton code. See examples on my GitHub account at https://github.com/ptd006. It is pretty self explanatory. The main limitation is that you can only read and write to existing variables and records.

Building C++ code from within Stata

When developing code (plugins, or otherwise) it can be handy to rebuild everything seamlessly from within Stata. In particular, you can have a .do analysis file that automatically recompiles your C++ if necessary.

I think the most convenient way to do this is run Stata from the Visual Studio tools command prompt, so all the compiler environment variables are defined. Then use Stata’s ability to execute shell commands to call either use MSBuild (to build a Visual Studio project) or nmake (if you have written a makefile for your project, as I typically do). I.e., from within Stata you would type:

!nmake

Write to a CSV file in your C++ file, and then load it in Stata

There are situations where Stata plugins are not convenient. For example:

  • They don’t appear to be thread safe. So, not really suitable for high performance applications.
  • If you have existing code it is a hassle to massage it to Stata’s plugin interface.
  • You may want your code to define variables dynamically, and produce a variable number of rows.
  • Etc.

In this case you might write data to a temporary file (possibly on a RAM drive) and then read it into Stata (unfortunately there seems no way to read directly from the standard output of a program in Stata). This is not elegant but works well in practice. Stata can read several formats. The most convenient for C++ output is CSV (comma separated values). You would do something like:

!myprogram > temp.csv

insheet using "temp.csv", clear

I’ve found it handy to combine this with the previous tip so that myprogram is automatically recompiled if the C++ source code has changed.