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: Calculating span exculsive of the current spell


From   Rebecca Pope <[email protected]>
To   [email protected]
Subject   Re: st: Calculating span exculsive of the current spell
Date   Mon, 10 Jun 2013 08:55:25 -0500

If you really want a span variable for each value of X, I'd use
-forvalues- rather than typing the same command with different -if-
conditions.

forvalues x = 1/3 {
   bys ID X (Start): gen span`x' = sum(End[_n-1] - Start[_n-1]) if X==`x'
}

list, noobs clean

     ID   Start    End   X   span1   span2   span3
    433    1987   1990   1       0       .       .
    433    1990   1992   1       3       .       .
    433    1994   1997   1       5       .       .
    433    1992   1994   2       .       0       .
    433    2000   2004   2       .       2       .
    433    1997   2000   3       .       .       0
    433    2004   2006   3       .       .       3

I'm not sure why you need to take up additional storage space by
having multiple span variables. If you decide you really don't need
multiple variables, you can just use a single line of code:

bys ID X (Start): gen span = sum(End[_n-1] - Start[_n-1])

For good general information about spans and durations, you might also
have a look at this FAQ from Nick Cox.
http://www.stata.com/support/faqs/data-management/identifying-runs-of-consecutive-observations/

Regards,
Rebecca

On Mon, Jun 10, 2013 at 7:50 AM, Kai Huang <[email protected]> wrote:
> Dear all,
>
> I have a spell dataset as follows:
> ID   Start   End     X
> 433  1987   1990     1
> 433  1990   1992     1
> 433  1992   1994     2
> 433  1994   1997     1
> 433  1997   2000     3
> 433  2000   2004     2
> 433  2004   2006     3
> where x is a dummy indicating activity status. I have the following commands for calculating the span of the respondent in each status:
> gen span=End-Start
> by ID: gen span1=sum(span) if X==1
> by ID: gen span2=sum(span) if X==2
> by ID: gen span3=sum(span) if X==2
> drop duration
> 3 new variables are added to the dataset:
> ID   Start   End     X   span1  span2  span3
> 433  1987   1990     1     3      .      .
> 433  1990   1992     1     5      .      .
> 433  1992   1994     2     .      2      .
> 433  1994   1997     1     7      .      .
> 433  1997   2000     3     .      .      3
> 433  2000   2004     2     .      6      .
> 433  2004   2006     3     .      .      5
> The above commands calculate the span including the current spell. I would like to know how can we calculate the span excluding the current spell so that we have the following values instead:
> ID   Start   End     X   span1  span2  span3
> 433  1987   1990     1     0      .      .
> 433  1990   1992     1     3      .      .
> 433  1992   1994     2     .      0      .
> 433  1994   1997     1     5      .      .
> 433  1997   2000     3     .      .      0
> 433  2000   2004     2     .      2      .
> 433  2004   2006     3     .      .      3
> Thank you very much in advance.
> Best regards,
> Kai Huang
> *
> *   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