Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

RE: st: Group identifiers within group


From   <isabel.lechner@vetsuisse.unibe.ch>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Group identifiers within group
Date   Fri, 7 Jun 2013 13:06:42 +0000

Thanks for the suggestions! But the input "egen level2 = group(GroupID AnimalID Parity)" just grouped the "GroupID", but not by Animal ID anymore (see output values of level2 below)

AnimalID		Parity	GroupID	level2
CH003381451070            	3	1		1
CH003381451070            	4	1		2
CH003381451070            	4	1		2
CH003381462076            	2	2		3
CH003381462076            	3	2		4
CH003381462076            	3	2		4
CH110003099108            	10	3		5
CH110003099108            	10	3		5
CH110003099108            	11	3		6
CH110003099108            	11	3		6
CH110003099108            	11	3		6
CH110003099108            	12	3		7
CH110003099108            	12	3		7
CH110003099108            	12	3		7

Also the suggestion by Alexis  "by AnimalID Parity: gen var1= _n"  didn't give me the right values.

AnimalID		Parity	GroupID	var1		
CH003381451070            	3	1		1
CH003381451070            	4	1		1
CH003381451070            	4	1		2
CH003381462076            	2	2		1
CH003381462076            	3	2		1
CH003381462076            	3	2		2
CH110003099108            	10	3		1
CH110003099108            	10	3		2
CH110003099108            	11	3		1
CH110003099108            	11	3		2
CH110003099108            	11	3		3
CH110003099108            	12	3		1
CH110003099108            	12	3		2
CH110003099108            	12	3		3

And here again what I'm trying to get, do you have any other suggestions?

 AnimalID                             Parity                    GroupID              2nd level?
CH003381451070             3                             1                             1
CH003381451070             4                             1                             2
CH003381451070             4                             1                             2
CH003381462076             2                             2                             1
CH003381462076             3                             2                             2
CH003381462076             3                             2                             2
CH110003099108             10                           3                             1
CH110003099108             10                           3                             1
CH110003099108             11                           3                             2
CH110003099108             11                           3                             2
CH110003099108             11                           3                             2
CH110003099108             12                           3                             3
CH110003099108             12                           3                             3
CH110003099108             12                           3                             3





-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: Freitag, 7. Juni 2013 14:17
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: Group identifiers within group

-group()- here is a very old and very undocumented function. I would suggest now to StataCorp, and I think I have suggested before, that calling -group()- should be illegal except under version control.

You are confusing it with the -egen- function of the same name.

I think you seek

egen level2 = group(GroupID AnimalID Parity)

Best not to use informative variable names such as "var1".

Nick
njcoxstata@gmail.com


On 7 June 2013 13:02,  <isabel.lechner@vetsuisse.unibe.ch> wrote:
> Dear all,
>
> I'm trying to create a new variable with a group identifier, but on a 2nd level.  For the variable "GroupID" I did the following:
>
> by AnimalID:  gen GroupID = _n
>
> Is it possible to do the same but on the 2nd level (based on AnimalID AND Parity), so that I will get the values as shown in the last variable "2nd level"?
> I've tried: by TierID: gen var1 = group(par), but this doesn't seem to do the job.  Data are sorted by AnimalID and then Parity.
>
>
> AnimalID                             Parity                    GroupID              2nd level?
> CH003381451070             3                             1                             1
> CH003381451070             4                             1                             2
> CH003381451070             4                             1                             2
> CH003381462076             2                             2                             1
> CH003381462076             3                             2                             2
> CH003381462076             3                             2                             2
> CH110003099108             10                           3                             1
> CH110003099108             10                           3                             1
> CH110003099108             11                           3                             2
> CH110003099108             11                           3                             2
> CH110003099108             11                           3                             2
> CH110003099108             12                           3                             3
> CH110003099108             12                           3                             3
> CH110003099108             12                           3                             3
>
> Thanks for your help!
> Isabel
>
> *
> *   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/
*
*   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/

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index