# Re: st: egen mean function

 From austin nichols To statalist@hsphsun2.harvard.edu Subject Re: st: egen mean function Date Wed, 17 Aug 2005 20:57:13 -0400

```The first egen should read, of course,
. egen mcnv=mean(cnv), by(nd)

and you will almost certainly want to clean up after the commands below:
. egen mean7day=max(m7cnv), by(nd)
. drop mcnv scnv cnv m7cnv nfirst

On 8/17/05, austin nichols <austinnichols@gmail.com> wrote:
> I assume you have numeric date nd, and compute mean cnv across
> individuals as, e.g.
> . egen mcnv=mean(cnv), by nd
>
> If you have data on every day, or you don't care if any are missing,
> and you really want the prior 7 calendar days, you could start with
> . bys nd: gen byte nfirst=(_n!=1)
> . sort nfirst nd
> . set obs `=_N+7'
> . replace nd=0 if nd==.
> . g byte s=0
> . forval d=1/7 {
>    replace scnv=max(0,scnv)+ /*
>  */    max(0,mcnv[_n-`d']*inrange(nd[_n-`d'],nd-7,nd-1)) if nfirst==0
>   }
> . g byte ccnv=0
> . forval d=1/7 {
>   replace ccnv=ccnv+(mcnv[_n-`d']!=. & inrange(nd[_n-`d'],nd-7,nd-1)!=0)
>   }
> . g m7cnv=scnv/ccnv
>
>
> On 8/17/05, James Avery <macavery@yahoo.com> wrote:
> > Dear all,
> >  I would like to create a new variable, prior
> > week campaign news volume (PWCNV) that is the mean CNV
> > for the prior 7 days. Is it possible to do this with
> > the egen mean function or with another egen function?
>

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