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]

Re: st: creation of a new variable using a variable condition


From   Carlos Avellaneda Suárez <[email protected]>
To   [email protected]
Subject   Re: st: creation of a new variable using a variable condition
Date   Fri, 21 Feb 2014 16:47:34 -0500

Once you have followed Nick's suggestion, a simple solution could be:

bysort id (year): gen newvar2 = sum(newvar)
replace newvar2=1 if event==1
l

2014-02-21 16:11 GMT-05:00 Riccardo Valboni <[email protected]>:
> Dear Nick,
>
> Many thanks for your answer!
>
> And what if I would like newvar to be a counting variable that counts the years from the last event(see example)? How would you do that?
>
> id      year    event   newvar
> a       2001    0       0
> a       2002    0       0
> a       2003    1       1
> a       2004    1       1
> b       2005    0       0
> b       2006    1       1
> b       2007    0       2
> b       2008    0       3
> b       2009    0       4
>
> Thank you.
> Riccardo
>
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: venerdì 21 febbraio 2014 16:14
> To: [email protected]
> Subject: Re: st: creation of a new variable using a variable condition
>
> One way to approach this is
>
> bysort id (year): gen newvar = sum(event) >= 1
>
> See also
>
> http://www.stata.com/support/faqs/data-management/first-and-last-occurrences/
>
>
> Nick
> [email protected]
>
>
> On 21 February 2014 15:04, Riccardo Valboni <[email protected]> wrote:
>> Dear statalist,
>>
>> I am trying to do something that it's probably basic but I cannot get to the bottom of it.
>> I have a typical unbalanced panel dataset. Some of my observations happen to have an event at a certain point in time (coded 1 in the 'event' column). For them I would like to generate a new variable (newvar) that takes value 1 from the year of the first event onwards. The year of the first event obviously changes from case to case. (see the example below)
>>
>> id      year    event   newvar
>> a       2001    0       0
>> a       2002    0       0
>> a       2003    1       1
>> a       2004    1       1
>> b       2005    0       0
>> b       2006    1       1
>> b       2007    0       1
>> b       2008    0       1
>> b       2009    0       1
>> ...
>>
>> I imagine that the command to do this starts with: by id... but I wouldn't know how continue that. Any suggestions?
>>
>> Thank you!
>> Riccardo
>>
>>
>>
>>
>> Disclaimer
>> ________________________________
>> De informatie verzonden in dit e-mail bericht is vertrouwelijk en is uitsluitend bestemd voor de geadresseerde van dit bericht. Lees verder: www.eur.nl/email-disclaimer <http://www.eur.nl/email-disclaimer>
>> The information in this e-mail message is confidential and may be legally privileged. Read more: www.eur.nl/english/email-disclaimer<http://www.eur.nl/english/email-disclaimer>
>> ________________________________
>>
>>
>> *
>> *   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/
>
> Disclaimer
> ________________________________
> De informatie verzonden in dit e-mail bericht is vertrouwelijk en is uitsluitend bestemd voor de geadresseerde van dit bericht. Lees verder: www.eur.nl/email-disclaimer <http://www.eur.nl/email-disclaimer>
> The information in this e-mail message is confidential and may be legally privileged. Read more: www.eur.nl/english/email-disclaimer<http://www.eur.nl/english/email-disclaimer>
> ________________________________
>
>
> *
> *   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index