Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: First occurence


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: First occurence
Date   Thu, 17 May 2007 23:24:27 +0100

Quite so. 

As you know, but some may not, 
the first year can always be used, without 
putting it into a variable, in ways like this

bysort bdsmaster (year) : gen years_since_first = year - year[1] 

Nick 
[email protected] 

Austin Nichols
 
> Nick--
> Yes, with the removal of the word "always" and the replacement of
> "fyr" with "minyr" --thanks for the correction... here is the danger
> of untested solutions, illustrated nicely, and a big part of the
> danger of not using -sysuse data- or equivalent at the start!
> The reason I proposed the two-line solution is the phrase "identify
> the first year that the idcode appear" which implies to me that Meryle
> Weinstein might need the year for other purposes, not just to make a
> first-year indicator.
> 
> On 5/17/07, Nick Cox <[email protected]> wrote:

> > Ho hum. I don't think Austin meant what he typed.
> > But I agree with his strong signal that you should
> > explain your data set-up clearly.
> >
> > The first year any id appears can be tagged by something like
> >
> > bysort bdsmaster (year) : gen first = _n == 1
> >
> > assuming that -bdsmaster- is an id code.
> >
> > Austin's equivalent is
> >
> > egen minyr = min(year), by(bdsmaster)
> > gen first = year == minyr
> >
> > but you can get there in one line.
> >
> > Nick
> > [email protected]
> >
> > Austin Nichols replied to Meryle Weinstein
> >
> > > It's always advisable to frame your question in terms of 
> data everyone
> > > can use, e.g. by starting your example with -sysuse auto- 
> and showing
> > > the result you want.  That said, one way (among many) to 
> generate an
> > > indicator for first year is:
> > >
> > > egen minyr=min(year), by(bdsmaster)
> > > gen fyr=(year==fyr)
> > >
> > > > I've tried following the syntax in FAQ and archives but am
> > > having difficulty
> > > > with it.  I have a dataset that has an idcode and a year
> > > for a number of
> > > > schools.  I want to identify the first year that the idcode
> > > appears.  I've
> > > > tried the following but still don't' get the first occurrence.
> > > >
> > > > by bdsmaster (year), sort: gen noccur = sum(bdsmaster)
> > > > by bdsmaster: gen byte first = noccur == 1  & noccur[_n -
> > > 1] != noccur
> > > >
> > > > gen byte first = sum(bdsmaster == 1) == 1  &
> > > sum(bdsmaster[_n - 1] == 1) ==
> > > > 0

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