Typing Python or R instead
Switch the prompt to Python or to R, on the same dataset, and switch back.
What we have
- a Python prompt on the same dataset, with
df,pdandnp - an R prompt running ten dplyr verbs, both pipes and both assignments
- changes made in either are seen by the next Stata command
- R mode follows R's missing-value rules, checked against a real R
- multi-line Python, including
forloops, inside a do-file
Coming later
- R mode is a subset, not an interpreter: no
across(), nocase_when, no window functions (lag,lead,row_number), notidyr, andleft_joinis the only join - no column helpers such as
starts_with()orwhere() - Stata's macros are not expanded in Python or R mode — deliberate, so a backtick or a
$in those lines is yours - R mode is multi-line only in Python's case; an R line stands alone
set language python set language stata
This is not an export. There is one dataset, and both languages are looking at it.
. sysuse vira.sample01, clear . set language python >>> df = df[df["age"] >= 15] >>> df["lnmpce"] = np.log(df["mpce"]) >>> set language stata . summarize lnmpce
The summarize at the end sees the rows Python dropped and the column Python made. Nothing was imported, copied or written to disk in between.
What you get at the Python prompt
df— the dataset, as a pandas DataFrame. Assign to it and the next Stata command sees the change.pdandnp— pandas and numpy, already imported.- Names persist: something made on one line is there on the next.
set language stata is understood at the Python prompt too. It is the way back, and it is not valid Python, so nothing else could mean it.
Two things to know
Stata's macros are not expanded in Python mode. A backtick or a $ in a Python line is yours to use.
The dataset is handed over with a plain 0-to-n index, because a Stata dataset is rows in order and has no index. If you set a meaningful index in Python — a groupby key, say — it comes back as an ordinary column, since that is the only place a Stata command could see it.
In a do-file
The lines after set language python are run as one piece of Python, so a for loop spread over several lines works. The mode returns to Stata at the end of the file.
R mode
set language r does the same thing for a subset of dplyr:
. set language r > df <- df %>% filter(age >= 15) %>% mutate(lnmpce = log(mpce)) > df %>% group_by(sector) %>% summarise(m = mean(mpce), n = n()) > set language stata . summarize lnmpce
No R is installed and none runs. The line is parsed here and carried out with pandas, on the same dataset. That is a different thing from set show r on, which prints the R for a Stata command without running it.
Ten verbs — filter, mutate, group_by, summarise, arrange, select, rename, count, distinct, left_join — plus ungroup, both pipes (%>% and |>), both assignments (<- and ->), and the functions you would put inside them.
R's rules apply in R mode, and they are not Stata's. With age of 20, 70, missing and 80:
| You type | Answer | Why |
|---|---|---|
count if age > 60 | 3 | Stata: missing is larger than any number |
filter(age > 60) | 2 | R: NA is not TRUE, so the row goes |
mean(x) is NA when anything is missing unless you write na.rm = TRUE, and a comparison against NA gives NA, so & and | carry it the way R does.
This subset is checked against a real R: the same dplyr line is run in R 4.6.1 with dplyr 1.2.1 and the numbers compared, every NA-sensitive case included.
Anything outside the subset is refused by name rather than guessed at.