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: Re: question about ids in a group


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: question about ids in a group
Date   Sun, 20 Mar 2011 12:33:29 +0900

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/


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