Good morning,

I recently discovered a nice feature of the user contributed -rangestat-: one can use it as a wrapper/executer/(executioner?) of Mata functions. I was wondering whether there is any other wrapper that can do this? Stata's native -statsby- cannot do it.

Example of what I have in mind is the following code:

Code:
. webuse nlswork, clear 
(National Longitudinal Survey.  Young Women 14-26 years of age in 1968)

.         mata:  
------------------------------------------------- mata (type end to exit) --------------------------------
:             mata clear

:             real rowvector mymean(real colvector X) {
>                 return(mean(X))
>             }

:         end 
----------------------------------------------------------------------------------------------------------

.  rangestat (mymean) ln_wage, interval(ln_wage . .) by(id) 

.  
.  egen mean = mean(ln_wage), by(id)

.  
.  summ mymean mean

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
     mymean1 |     28,534    1.674907    .3780522          0   3.912023
        mean |     28,534    1.674907    .3780522          0   3.912023

.
I have two issues with the solution above involving -rangesta-

1. I seem to be misusing the command a bit, it does not seem to be intended for this purpose, as it requires the -, interval()- option, and I am sort of overriding this.

2. In other tests unreported here, I found this solution involving -rangestat- to be substantially slower than an explicit loop in which the Mata function is embedded, of the sort that Daniel Klein shows in #3, or Leonardo Guizzetti shows in #2 on this thread here: https://www.statalist.org/forums/for...d-of-a-command

In short, is there no any other wrapper apart from -rangestat- that can run a Mata function by groups? And is there anything that I can do to speed up the execution above using -rangestat-?