Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: make a program byable

From   Gary Longton <>
Subject   Re: st: make a program byable
Date   Sun, 23 Jun 2002 23:36:40 -0700

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


> . . . 
> 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.


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'

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.

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'

- Gary
*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index