python stata交互

python:
from sfi import Scalar
def calcsum(sum1, sum2):
    res = sum1 + sum2
    Scalar.setValue("result", res) #存入 scalar
a=1
b=2
calcsum(a, b)
end
//python函数建立一次,只要不关闭STATA则可一直使用
display result
python
calcsum(4,5)
end
disp result

//stata调用python .py文件
local a=1
local b=2
python script c:\python37\statax.py
disp result
python script c:\python37\statax1.py,args(`a',`b')
disp result
sysuse auto
python
from sfi import Data
#dataraw=Data.get('foreign')
dataraw=Data.get('foreign mpg rep78',range(46,56),valuelabel=True)
dataraw
end
// >>> dataraw
// [['Domestic', 18, 4], ['Domestic', 18, 1], ['Domestic', 19, 3], ['Domestic', 19, 3], ['Domestic', 19, 8.98
// > 846567431158e+307], ['Domestic', 24, 2], ['Foreign', 17, 5], ['Foreign', 23, 3], ['Foreign', 25, 4], ['F
// > oreign', 23, 4]]
// >>> end

 

posted @ 2023-04-16 12:35  myrj  阅读(20)  评论(0编辑  收藏  举报