As the title suggested, I would like to count the number of variables in multiple locals and meanwhile to compute the mean value of each group in Stata. For example,
I can use the following Stata code to compute total scores for each variable group, However, I don't know how to compute the mean value and don't want to do the hard coding
by counting the number of variables by hand.

*Compute total score of sub-scales.
local x1 ptgi_6 ptgi_8 ptgi_9 ptgi_15 ptgi_16 ptgi_20 ptgi_21
local x2 ptgi_3 ptgi_7 ptgi_11 ptgi_14 ptgi_17
local x3 ptgi_4 ptgi_10 ptgi_12 ptgi_19
local x4 ptgi_5 ptgi_18
local x5 ptgi_1 ptgi_2 ptgi_13
forvalue i = 1/5 {
egen ptgi_domain_`i'_sum = rowtotal( `x`i''), missing
}

Any help or hint is highly appreciated!