# st: RE: using creat count id on var

 From "Nick Cox" To Subject st: RE: using creat count id on var Date Sun, 27 Feb 2005 19:50:36 -0000

```This field centres on the use of -by:-.

One attempt at an introductory overview
emphasising the basics very strongly is

How to move step by: step.
Stata Journal 2(1): 86-102 (2002).

This explains the use of the -by:- construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N.

In addition, many data management and other FAQs
contain examples of -by:-.

In your case, you are over the last ridge and home
is nearly in sight. As Joseph Coveney suggested,
you also should tap into -egen- for handy functions.

Again, there is propaganda in an overview

On getting functions to do the work.
Stata Journal 2(4): 411--427 (2002).

egen max1 = max(var1), by(id visitno)
egen mean1 = mean(var1), by(id visitno)

or

bysort id visitno: egen max1 = max(var1)
by id visitno: egen mean1 = mean(var1)

Nick
n.j.cox@durham.ac.uk

Richard Hiscock

> Im obviously finding it difficult to develop in this area
> I cant find a useful guide,any pointers?
>
> My problem now is that having identified the visitno within
> id I wish to
> perform manipulations on a variable. For example
> using the illustrated structure.
> To obtain the maximum value of var1 within id at each level
> of visitno.
> To obtain the mean value of var1 within id at each level of visitno
>
>     id           date          visitno      var1
> >     1        16267         1             53
> >     1        16267         1            106
> >     1        16267         1            84
> >     1        16267         1            90
> >     1        16267         1            76
> >     1        16267         1            65
> >     1        16298         2             87
> >     1        16298         2            76
> >     2        15678         1            98
> >     2        15678         1            98
> >     2         16541        2            150

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