Differences between revisions 2 and 12 (spanning 10 versions)
Revision 2 as of 2019-12-18 15:56:19
Size: 1262
Comment:
Revision 12 as of 2023-06-09 15:35:23
Size: 2721
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
= Setup = = Stata Python =
Line 3: Line 3:
The path to the Python executable is set using: Stata supports calling out to an embedded [[Python]] interpretter.
Line 5: Line 5:
{{{
python set exec "path_string"
}}}
<<TableOfContents>>
Line 9: Line 7:
Stata can list recognized Python environments with `python search`.

To make this setting permanent, use the `permanent` option.
----
Line 15: Line 11:
== Installation ==

Most system configuration is done with the '''`python set`''' command.

Stata can list recognized Python environments with `python search`. To add an unrecognized environment, try:

{{{
python set exec "C:\path\to\python\installation"
}}}

To prepend or append to the `PYTHONPATH`, use:

{{{
python set userpath "C:\foo" "C:\bar" "C:\baz", [prepend]
}}}

To make these settings permanent, add the '''`permanent`''' option.
Line 17: Line 31:
= Python REPL =
Line 19: Line 32:
A Python REPL is entered with the `python` command. It prints to the screen a reminder that the `end` command is used to exit the environment.
== Usage ==



=== REPL ===

A Python REPL is entered with the `python` command alone. It prints to the screen a reminder that the `end` command is used to exit the environment.
Line 34: Line 54:
Within the REPL, the `stata` context submits commands to the parent Stata shell. Within the REPL, prefix a Stata command with `stata:`.

{{{
>>> stata: webuse auto, clear
}}}
Line 38: Line 62:
----

= Python Scope =

A Python scope can be entered with `python:` (similar to defining function in Stata). Then Python environment is exited upon leaving the scope.



----

= Python SFI Module =
=== SFI ===
Line 55: Line 69:
# do something
Line 57: Line 72:
This can even be used within the Python REPL that is running within the Stata shell. This can be used within a REPL as shown above.



=== Scope ===

A '''Python scope''' can be entered with `python:`. This Python environment lasts only until the end of the statement.

To submit multiple statements on a scope, delimit them with semicolons.



=== Do Files ===

While an interactive `python:` scope lasts only until the end of the statement, a do file `python:` scope continues until explicitly terminated by the `end` command.

{{{
webuse auto
python:
from sfi import Data
pymake = Data.get(’make’)
# do something
end
}}}

A common design pattern is to define Python functions within an [[Stata/AdoFiles|ado file]] with the primary Stata function being a thin interface for them.

{{{
program varsum
    version 16.0
    syntax varname [if] [in]

    python: _varsum("`varlist'", "`touse'")
    display as txt " sum of ‘varlist’: " as res r(sum)
end

python:
from sfi import Data, Scalar
def _varsum(varname, touse):
    x = Data.get(varname, None, touse)
    Scalar.setValue("r(sum)", sum(x))
end
}}}

{{{
. webuse auto
(1978 Automobile Data)

. varsum price
sum of price: 456229

. varsum price if foreign
sum of price: 140463

}}}

----



== See also ==

[[https://www.stata.com/manuals/ppystataintegration.pdf|Stata manual on PyStata integration]]

Stata Python

Stata supports calling out to an embedded Python interpretter.


Installation

Most system configuration is done with the python set command.

Stata can list recognized Python environments with python search. To add an unrecognized environment, try:

python set exec "C:\path\to\python\installation"

To prepend or append to the PYTHONPATH, use:

python set userpath "C:\foo" "C:\bar" "C:\baz", [prepend]

To make these settings permanent, add the permanent option.


Usage

REPL

A Python REPL is entered with the python command alone. It prints to the screen a reminder that the end command is used to exit the environment.

Stata local variables are accessed with quotations.

. local int_var = 3
. local str_var = "This is a Stata string"
. python
---------------------------------------- python (type end to exit) -----------
>>> `int_var'
3
>>> "`str_var'".split(" ")
['This', 'is', 'a', 'Stata', 'string']

Within the REPL, prefix a Stata command with stata:.

>>> stata: webuse auto, clear

SFI

A foreign function interface module is also available.

from sfi import Data
pymake = Data.get('make')
# do something

This can be used within a REPL as shown above.

Scope

A Python scope can be entered with python:. This Python environment lasts only until the end of the statement.

To submit multiple statements on a scope, delimit them with semicolons.

Do Files

While an interactive python: scope lasts only until the end of the statement, a do file python: scope continues until explicitly terminated by the end command.

webuse auto
python:
from sfi import Data
pymake = Data.get(’make’)
# do something
end

A common design pattern is to define Python functions within an ado file with the primary Stata function being a thin interface for them.

program varsum
    version 16.0
    syntax varname [if] [in]

    python: _varsum("`varlist'", "`touse'")
    display as txt " sum of ‘varlist’: " as res r(sum)
end

python:
from sfi import Data, Scalar
def _varsum(varname, touse):
    x = Data.get(varname, None, touse)
    Scalar.setValue("r(sum)", sum(x))
end

. webuse auto
(1978 Automobile Data)

. varsum price
sum of price: 456229

. varsum price if foreign
sum of price: 140463


See also

Stata manual on PyStata integration


CategoryRicottone

Stata/Python (last edited 2025-10-24 16:20:01 by DominicRicottone)