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   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: creation of a new variable using a variable condition
Date   Sat, 22 Feb 2014 13:26:18 +0000

Check out -tsspell- (SSC) and

SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata: Identifying spells
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/07   SJ 7(2):249--265                                 (no commands)
        shows how to handle spells with complete control over
        spell specification

http://www.stata-journal.com/sjpdf.html?articlenum=dm0029

Nick
[email protected]


On 22 February 2014 13:15, Riccardo Valboni <[email protected]> wrote:

> The solution shown below works, but not completely. If a second event occurs, the count does not restart. The first year of the second event is substituted with a 1 by the replace command but the years after the second event are not recomputed.
>
> Can someone suggest a solution for this?

Carlos Avellaneda Suárez

> 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

Riccardo Valboni

>> 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

Nick Cox

>> 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-occur
>> rences/

Riccardo Valboni

>>> 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?

*
*   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