Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Flagging most frequent occurrence


From   Steven Archambault <[email protected]>
To   [email protected]
Subject   st: Flagging most frequent occurrence
Date   Thu, 24 Oct 2013 00:03:25 -0600

Hi folks,

I have a pretty basic question, but cannot seem to find a
straightforward answer.

I have panel data, where observations occur in different years. I want
to flag the year that occurs the most often.

Here is what it looks like, with the flag I want.

id year flag
1 2008 0
1 2008 0
1 2009 1
2 2009 1
2 2009 1
2 2010 1
2 2010 1
3 2009 0
3 2009 0
3 2010 1


What is the best way to do this? I have an around about way to do this,
egen countyr=count(year), by(id year)
egen maxcount=max(countyr), by(id)
gen most_freq=0
replace most_freq=year if maxcount==countyr
gen flag=0
replace flag=1 most_freq==year

But seems there would be a problem with it if two years occur with
equal frequency.
In that case, I would want both years to be flagged.

Any thoughts?

Thanks,
Steve
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index