From what I can tell, python code cannot be run inside a loop (forvalue, foreach..). Instead I tried to embed the code within a program function definition. Following the most basic example from the help menu, I still cannot get the code to run. I can get the python code running fine without embedding the code (for a single iteration of many). It can't seem to find the calcsum def. Any one else have this problem or see the issue?
Error:
Thanks!
Code:
cap program drop varsum
program define varsum
version 16.0
syntax varname [if] [in]
marksample touse
di "`varlist'"
ta `touse', m
python: calcsum("`varlist'", "`touse'")
display as txt " sum of varlist: " as res r(sum)
end
version 16.0
python:
from sfi import Data, Scalar
def calcsum(varname, touse):
x = Data.get(varname, None, touse)
Scalar.setValue("r(sum)", sum(x))
end
sysuse auto, clear
varsum price
Code:
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'calcsum' is not defined

Comment