Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: Re: question about ids in a group |
Date | Sun, 20 Mar 2011 10:22:44 +0000 |
I wonder if you missed the -unique- option within -egen, rank()-. Nick On Sun, Mar 20, 2011 at 3:41 AM, Pat R <profanepolicy@gmail.com> wrote: > Thank you Joseph, this is exactly what I wanted! Now to figure out how > exactly it works :) > On Sat, Mar 19, 2011 at 11:33 PM, Joseph Coveney <jcoveney@bigplanet.com> wrote: >> >> Pat R wrote: >> >> I have a panel dataset from which I'm trying to make a smaller dataset >> with new identifiers. My (new) dataset is organised this way right >> now: >> >> QID | Year | PersonID >> >> 1 | 2009 | 3 >> 1 | 2009 | 4 >> 2 | 2009 | 9 >> 2 | 2009 | 10 >> 2 | 2010 | 9 >> 2 | 2010 | 10 >> 2 | 2009 | 11 >> >> I want to create new identifiers for each person (consistent across >> years), starting from 1, within each QID. I tried using the -egen >> group- function, but since I cannot combine that with -by-, it creates >> unique values which ignore the QID. I tried using -egen rank-, but >> that of course skips a rank if two IDs are the same. I'm sure there's >> a terribly easy way to do this, but I just can't see it. Can anyone >> help? (It's my first time asking a question, so I apologize if I've >> left out important information) >> >> -------------------------------------------------------------------------------- >> >> If I understand you correctly, then it would be something like that illustrated >> below. If that's not what you want, then write back to the list with a fourth >> column in you example that shows just what you want as the result. >> >> Joseph Coveney >> >> . version 11.1 >> >> . >> . clear * >> >> . set more off >> >> . >> . input long (QID Year PersonID) >> >> QID Year PersonID >> 1. 1 2009 3 >> 2. 1 2009 4 >> 3. 2 2009 9 >> 4. 2 2009 10 >> 5. 2 2010 9 >> 6. 2 2010 10 >> 7. 2 2009 11 >> 8. end >> >> . >> . * >> . * Begin Here >> . * >> . bysort QID PersonID: generate long new_id = _n == 1 >> >> . quietly by QID: replace new_id = sum(new_id) >> >> . list, noobs sepby(QID) >> >> +--------------------------------+ >> | QID Year PersonID new_id | >> |--------------------------------| >> | 1 2009 3 1 | >> | 1 2009 4 2 | >> |--------------------------------| >> | 2 2010 9 1 | >> | 2 2009 9 1 | >> | 2 2009 10 2 | >> | 2 2010 10 2 | >> | 2 2009 11 3 | >> +--------------------------------+ >> >> . exit >> >> end of do-file * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/