|
Size: 1262
Comment:
|
Size: 2442
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 1: | Line 1: |
| = Setup = | = Stata Python = <<TableOfContents>> ---- == Installation == |
| Line 11: | Line 19: |
| To make this setting permanent, use the `permanent` option. | To prepend or append to the `PYTHONPATH`, use: {{{ python set userpath "path_string" [...], [prepend] }}} To make these settings permanent, use the `permanent` option. ---- |
| Line 15: | Line 31: |
| ---- | == Usage == |
| Line 17: | Line 33: |
| = Python REPL = | === Python REPL === |
| Line 36: | Line 52: |
---- = 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. |
{{{ >>> stata: sysuse auto, clear }}} |
| Line 46: | Line 58: |
| ---- | === Python Scope === |
| Line 48: | Line 60: |
| = Python SFI Module = | A Python scope can be entered with `python:` (similar to defining function in Stata). This Python environment is exited upon leaving the scope, i.e. the end of the statement. To submit multiple statements, delimit them with semicolons. === Python SFI Module === |
| Line 55: | Line 71: |
| # do something | |
| Line 61: | Line 78: |
| === Stata Do Files === Python scopes within a Do file are entered with `python:` and terminated with `end`. {{{ sysuse auto, clear python: from sfi import Data pymake = Data.get(’make’) # do something end }}} A common strategy is to define Python functions within a Do file, then create Stata functions to interface. {{{ 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 }}} {{{ . sysuse auto, clear (1978 Automobile Data) . varsum price sum of price: 456229 . varsum price if foreign sum of price: 140463 }}} |
Stata Python
Installation
The path to the Python executable is set using:
python set exec "path_string"
Stata can list recognized Python environments with python search.
To prepend or append to the PYTHONPATH, use:
python set userpath "path_string" [...], [prepend]
To make these settings permanent, use the permanent option.
Usage
Python REPL
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.
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, the stata context submits commands to the parent Stata shell.
>>> stata: sysuse auto, clear
Python Scope
A Python scope can be entered with python: (similar to defining function in Stata). This Python environment is exited upon leaving the scope, i.e. the end of the statement. To submit multiple statements, delimit them with semicolons.
Python SFI Module
A foreign function interface module is also available.
from sfi import Data
pymake = Data.get('make')
# do somethingThis can even be used within the Python REPL that is running within the Stata shell.
Stata Do Files
Python scopes within a Do file are entered with python: and terminated with end.
sysuse auto, clear python: from sfi import Data pymake = Data.get(’make’) # do something end
A common strategy is to define Python functions within a Do file, then create Stata functions to interface.
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. sysuse auto, clear (1978 Automobile Data) . varsum price sum of price: 456229 . varsum price if foreign sum of price: 140463
