program varavg
    version 16.0
    syntax varname [if] [in]
    
	marksample touse
    python: calcsum("`varlist'", "`touse'")
    display as txt " mean of `varlist': " as res r(avg)
end

python:
from sfi import Data, Scalar
def calcsum(varname, touse):
    x = Data.get(varname, None, touse)
    Scalar.setValue("r(avg)", sum(x)*1.0/len(x))
end