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   Kai Huang <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: Calculating span exculsive of the current spell
Date   Mon, 10 Jun 2013 20:51:08 +0000

Dear Rebecca,

Thank you for your reply. The span variables are actually previous experiences of different types of employment (full-time, part-time work, etc). I can get what I want now but further problem arises.The data now becomes like this after carrying out a series of commands:

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

My final aim is to recode the missing values as follows:

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

I type the following command: "by ID: replace span1=span1[_n-1]+End[_n-1]-Start[_n-1] if missing(span1)", but the values become something like this:

ID  Start End  X span1 span2 span3
433 1987  1990 1   0     0     0
433 1990  1992 1   3     0     0
433 1992  1994 2   5     0     0
433 1994  1997 1   5     2     0
433 1997  2000 3  (8)    2     0
433 2000  2004 2  (11)   2     3
433 2004  2006 3  (15)  (6)    3

I wonder how we can solve this problem. Thank you very much.


----------------------------------------
> From: [email protected]
> Date: Mon, 10 Jun 2013 08:55:25 -0500
> Subject: Re: st: Calculating span exculsive of the current spell
> To: [email protected]
>
> 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/ 		 	   		  
*
*   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