stata调用python函数,利用函数返回值

//stata调用python,引用py文件中的函数
//stata中设置python路径
set python_exec d:\python37\python.exe
//stata中调用python
python
//引入函数
from myfunction1 import add_numbers
//调用函数
add_numbers(3, 4)
//从python返回到stata
end
//在stata中显示变量的值
disp result
//在stata中打开表
webuse auto,clear
//增加变量ab,其值是python返回的值
gen ab=result


//stata中手工输入python代码
python:
from sfi import Scalar
def calcsum(sum1, sum2):
    res = sum1 + sum2
    Scalar.setValue("result", res)
calcsum(7, 4)
end
disp result
webuse auto,clear
gen aa=result


//stata中检索python的安装路径
python search
// ---------------------------------------------------------------------------------------------------------------------
//  Python environments found:  
//  D:\Python38\python.exe
//  D:\Python37\python.exe
// --------------------------------------------------------------------------------------------------------------------
//stata查询python相关信息
python query
// ---------------------------------------------------------------------------------------------------------------------
//     Python Settings
//       set python_exec      d:\python37\python.exe
//       set python_userpath  
//
//     Python system information
//       initialized          yes
//       version              3.7.0
//       architecture         64-bit
//       library path         d:\python37\python37.dll

//stata中清除python的变量及函数
python clear

//在STATA中显示python设置的变量及函数
python des
// add_numbers: 
// <function add_numbers at 0x0000000006BAB950>
//
// result: 
// 7

 

posted @ 2023-03-18 10:35  myrj  阅读(166)  评论(0编辑  收藏  举报