3 Stata Engine Path

3.1 Introduction

In order to put together dynamic documents (Markdown) that process Stata code from within R or RStudio, the knitr function needs the location of Stata's executable (the program or app file).This is not something most of us usually think about.

3.2 Loading Statamarkdown

In a preliminary code block in your document, load the Statamarkdown package.

In addition to loading the Statamarkdown package, you may need to specify the location of the Stata executable. An attempt has been made to automate this for recent versions of Stata (>= Stata 11) that are installed in the default location.

If Stata is found successfully, you will see a message (or messages) like

library(Statamarkdown)
Stata found at C:/Program Files/Stata18/StataSE-64.exe
The 'stata' engine is ready to use.

(If you have more than one version of Stata installed on your computer, the last one listed will be used.)

If Statamarkdown fails to find your Stata program, you will see the message

No Stata executable found.

3.3 Finding the Stata program

If you need to give this location yourself, an easy way to figure it out is to ask your Stata software!

Start up Stata, and in Stata issue the command

sysdir

Older versions of Stata this required an interactive Stata session to do this. In a batch environment (like Statamarkdown) the resulting file paths can by cryptic, so doing this in an interactive Stata session is best.

For a typical Windows installation, the results would similar to this:

   STATA:  C:\Program Files\Stata18\
    BASE:  C:\Program Files\Stata18\ado\base\
    SITE:  C:\Program Files\Stata18\ado\site\
    PLUS:  u:\stata\ado\plus\
PERSONAL:  u:\stata\ado\personal\
OLDPLACE:  c:\ado\

The line labeled STATA: is the folder where your Stata executable is located (possibly not the same as shown here). You can browse there with your computer's file explorer to see the actual file name of the Stata executable, which you need.

(Notice that BASE and SITE also clue you in, if you are working in Stata batch mode.)

If you are working from a linux command line, you can try

> which stata

3.4 Loading Statamarkdown (again)

I am usually using a 64-bit version of Stata SE, so had this not been found initially I would modify the preliminary code chunk in my document to read

library(Statamarkdown)
stataexe <- "C:/Program Files/Stata18/StataSE-64.exe"
knitr::opts_chunk$set(engine.path=list(stata=stataexe))

Notice for Windows I have switched backslashes to forward slashes!

On a Mac you might try something like

stataexe <- "/Applications/Stata/StataSE.app/Contents/MacOS/StataSE"

3.5 Example

And from there you are ready to execute and write about Stata code!

sysuse auto
summarize
(1978 automobile data)

    Variable |        Obs        Mean    Std. dev.       Min        Max
-------------+---------------------------------------------------------
        make |          0
       price |         74    6165.257    2949.496       3291      15906
         mpg |         74     21.2973    5.785503         12         41
       rep78 |         69    3.405797    .9899323          1          5
    headroom |         74    2.993243    .8459948        1.5          5
-------------+---------------------------------------------------------
       trunk |         74    13.75676    4.277404          5         23
      weight |         74    3019.459    777.1936       1760       4840
      length |         74    187.9324    22.26634        142        233
        turn |         74    39.64865    4.399354         31         51
displacement |         74    197.2973    91.83722         79        425
-------------+---------------------------------------------------------
  gear_ratio |         74    3.014865    .4562871       2.19       3.89
     foreign |         74    .2972973    .4601885          0          1

This page was written using:

  • Statamarkdown version 0.9.2
  • knitr version 1.45