Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: construct new id variable


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: construct new id variable
Date   Thu, 11 Sep 2003 10:35:59 +0100

Oleksandr Shepotylo
 
> I have a survey data with following variables:
> 
> familyid  personid  inwork
> 001         01            0
> 001         02            1
> 001         03            0
> 001         04            1
> 002         01            1
> 002         02            0
> 
>     Inwork is indicator of labor force partisipation. I 
> want to create
> variable "inworkid" that will be 0 if inwork=0 and will 
> give numbers 1, 2,3
> etc otherwise. In my example:
> 
> familyid  personid  inwork  inworkid
> 001         01            0            0
> 001         02            1            1
> 001         03            0            0
> 001         04            1            2
> 002         01            1            1
> 002         02            0            0
> 
> How can this be done?

bysort familyid inwork : gen inworkid = cond(inwork == 0, 0, _n) 

The key to this is that _n is interpreted with reference 
to each distinct group defined by the <byvarlist> of 
-by <byarlist>:-. For more information, see the various 
entries on -by:- in the manuals. Alternatively, one 
place in which the main ideas are discussed together
is Stata Journal 2(1):86-102 (2002). 

Nick 
[email protected] 
*
*   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