*! version 1.0.0
program varsum
	syntax varname [if] [in]
	marksample touse
	mata: calcsum("`varlist'", "`touse'")
	display as txt "   sum = " as res r(sum)
end

mata:
void calcsum(string scalar varname, string scalar touse)
{
	real colvector     x
	real scalar        sum, i

	st_view(x, ., varname, touse)

	sum = 0
	for (i=1; i<=rows(x); i++) {
		sum = sum + x[i]
	}

	st_numscalar("r(sum)", sum)
}
end