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

st: RE: make a program byable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: make a program byable
Date   Mon, 24 Jun 2002 10:09:35 +0100

Katsuhide Isa
> 
> 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
> 
> 'bys year : coefvar basepay'
> 
> using a data set containing multiple panels, following
> results:are shown on the screen:
> 
> __________________________________________________________________
> __________
> ___
> -> year = 1989
>        3.7137332
> 
> __________________________________________________________________
> __________
> ___
> -> year = 1990
>        3.7137332
> 
> __________________________________________________________________
> __________
> ___
> -> year = 1991
>        3.7137332
> 
> __________________________________________________________________
> __________
> ___
> -> year = 1992
>        3.7137332
> 
> __________________________________________________________________
> __________
> ___
> -> year = 1993
> ....
> 
> and so on. 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, but I'm sorry I can't fix it.
 
Gary Longton replied to the question directly. 

Two ther comments are that 

sd / mean 

is the definition of coefficient of variation in the literature 
I have encountered and that 

egen mean = mean(basepay), by(year) 
egen sd = sd(basepay), by(year) 
gen cv = sd / mean 
tabdisp year, c(cv) 

gets you there interactively. 

Nick 
[email protected] 

*
*   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/



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