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

Re: st: creating count within id & date


From   Joseph Coveney <jcoveney@bigplanet.com>
To   Statalist <statalist@hsphsun2.harvard.edu>
Subject   Re: st: creating count within id & date
Date   Sun, 27 Feb 2005 15:14:14 +0900

Richard Hiscock wrote:

I have the following data structure

    id       date        var1        var2

    1        16267         x         53
    1        16267         y        106
    1        16267         y        84
    1        16267         y        90
    1        16267         z         76
    1        16267         z         65
    1        16298         x         87
    1        16298         y        76
    2        15678         x        98
    2        15678         y        98
    2         16541        x        150

etc
I wish to create identifier (visitno) 1,2,3,... for each sequential date
within id.
I have tried to use :
sort id date
by id date: gen visitno = [_n]  which obviously doesnt produce the
required variable
I would be greatful for solution & ?or direction to FAQ dealing with
this

----------------------------------------------------------------------------

How about:

-by id date: generate byte visitno = _n == 1-

and then

-by id: replace visitno = sum(visitno)-

Or, in one line:

-bysort id (date): generate byte visitno = ///
  sum( (_n == 1) | (date > date[_n-1]) )-

As I recall, Nick Cox wrote a posted a more elegant one-line command for
this task recently on the list, but I cannot seem to find it.

You mention that your final aim is to generate summary statistics each date
within id.  For this, check out -egen-.

Joseph Coveney


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index