> generate byte total_sum =0
>
> for each variable of varlist {
> if age > 50
> total_sum = total_sum + age
> }
>
This looks like syntax from some alternative software where you have
to loop over the observations. You never have to do that in Stata to
generate a summary statistic. If what you mean is the sum of the
observations on age conditional on age>50,
summarize age if age>50
scalar sumage = r(sum)
display "The sum is " sumage
If you are trying to compute a number, store it as a scalar, not in a
new Stata variable.