Katsuhide Isa wrote:
> Recently I wrote the following simple program
> titled 'coefvar.ado' for the purpose of calculating
> coefficients of variation.
>
> ----------coefvar.ado----------
> program define coefvar, rclass byable(recall)
> version 7
> qui summ `1', det
> local coefvar = r(mean)/r(sd)
> di in green _col(8) `coefvar'
> end
> ----------coefvar.ado----------
>
> But somehow it doesn't work as expected.
> More specifically, when typing like
<cut>
> . . .
> Namely, they are all the same for
> each year when they should vary from year
> to year.
> I guess something is wrong or missing with this
> program, . . .
See -help byable- or the manual: [P] byable
Your program is missing a -marksample- or -mark- command, the means by which a -byable(recall)- program keeps track of and restricts to each by-group.
Eg.:
----------coefvar2.ado----------
program define coefvar2, byable(recall)
version 7
marksample touse /* added line.
marksample knows to mark wrt by-group
in addition to handling -syntax [if] [in]-
restrictions if included */
qui summ `1' if `touse' /* restrict to marked sample with the marker variable `touse' */
local coefvar = r(mean)/r(sd)
di in green _col(8) `coefvar'
end
----------coefvar2.ado----------
additional notes:
The -rclass- program option is not necessary as your program does not return any results.
The -,detail- option of -summarize- is not necessary to obtain mean and sd
Consider adding a -syntax- command, as -marksample- will neatly handle any sample restrictions included there in additions to keeping track of the by-groups.
Eg.:
----------coefvar3.ado----------
program define coefvar3, byable(recall)
version 7
syntax varname [if] [in]
marksample touse
qui summ `1' if `touse'
local coefvar = r(mean)/r(sd)
di in gr " coef. of variation: " in yel _col(8) `coefvar'
end
----------coefvar3.ado----------
- Gary
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/