Good afternoon,

I am trying to write an egen function that takes one newvar as an input, but generates two variables having newvar as a stub. For concreteness say that I want to generate both the mean and the count of observations, so that I write:

Code:
egen pricestats = temp(price), by(rep)
and this function produces two new variables, pricestats_mean and pricestats_nobs.

This is my ado file:

Code:
cap prog drop _gtemp
program define _gtemp
        version 11, missing
        syntax newvarname =/exp [if] [in] [, BY(varlist)]

        tempvar touse 
        quietly {
                gen byte `touse'=1 `if' `in'
                sort `touse' `by'
                by `touse' `by': gen `typlist' `varlist'_mean = /*
                        */ sum(`exp')/sum((`exp')<.) if `touse'==1
                by `touse' `by': gen `typlist' `varlist'_nobs = /*
                        */ sum((`exp')<.) if `touse'==1        
                
                by `touse' `by': replace `varlist'_mean = `varlist'_mean[_N]
                by `touse' `by': replace `varlist'_nobs = `varlist'_nobs[_N]
                
        }
end
and this is the errors that it generates:

Code:
. set trace off

. sysuse auto
(1978 Automobile Data)

. keep price rep

egen pricestats = temp(price), by(rep)

*** OUTPUT OMITTED***

 ----------------------------------------------------------------------------------------- end _gtemp ---
- global EGEN_SVarname
- global EGEN_Varname
- if _rc { exit _rc }
- quietly count if missing(`dummy')
= quietly count if missing(__000001)
__000001 ambiguous abbreviation
--------------------------------------------------------------------------------------------- end egen ---
r(111);

. des

Contains data from C:\Program Files (x86)\Stata15\ado\base/a/auto.dta
  obs:            74                          1978 Automobile Data
 vars:             4                          13 Apr 2016 17:45
 size:           888                          (_dta has notes)
----------------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
----------------------------------------------------------------------------------------------------------
price           int     %8.0gc                Price
rep78           int     %8.0g                 Repair Record 1978
__000001_mean   float   %9.0g                 
__000001_nobs   float   %9.0g                 
------------------------------------------
First my function seems to work, but somebody is complaining about I do not know what... Secondly, the stub of the newly generated variables is wrong.