Thank you all
I thought bysort id: gen x = cond( _n < 4, 1, 2) was rather elegant. I
don't have an obs variable but it appears to be stable.
On Thu, Mar 27, 2008 at 8:07 PM, Kit Baum <[email protected]> wrote:
> Sergiy said
>
> > Raphael said
> >
> > I have 100 subjects with 6 obs each. I would like to create a new
> > variable containing 1s for the first three obs and 2s for the
> > remaining obs for each id.
> >
> >
> > bysort id: gen x = cond( _n < 4, 1, 2)
>
> and this raises the question, is _bysort_ stable? Because if it is
> not, then not the first three of Raphael's observations get 1s, but
> three random of each six get 1s. Stata's manual is silent about it,
> but it seems to be the case. Quote:
>
> "sort specifies that if the data are not already sorted by varlist, by
> sort them."
>
> Does anybody have more precise info? Is bysort stable?
>
>
>
> This can be dealt with, presuming Raphael has an obs number for each
> reading on a subject, by doing
>
> bysort id (obs): gen x ...
>
> which will ensure that the observations for each ID are ordered
> 1,2,...,6.
>
>
>
> Kit Baum, Boston College Economics and DIW Berlin
> http://ideas.repec.org/e/pba1.html
> An Introduction to Modern Econometrics Using Stata:
> http://www.stata-press.com/books/imeus.html
>
>
> *
> * 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/
>
*
* 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/