Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Rebecca Pope <rebecca.a.pope@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Calculating span exculsive of the current spell |

Date |
Tue, 11 Jun 2013 09:06:17 -0500 |

Kai, I'm going to assume you mean you want negative values when you list (8) and (6). You don't show your "series of commands" to produce the 0s, but one is sufficient. Please note that the -bysort ID X (Start)- is necessary. It places the observations in order by type of unemployment (I'm assuming that's what X means) and the date the spell started. Otherwise, you will get incorrect sums, as you did in the results you posted. *** begin code *** forval x = 1/3 { bys ID X (Start): gen span`x' = sum(End[_n-1] - Start[_n-1]) if X==`x' replace span`x' = 0 if X < `x' } mvencode span1, mv(-8) mvencode span2, mv(-6) list ID Start End X span?, clean noobs *** output *** ID Start End X span1 span2 span3 433 1987 1990 1 0 0 0 433 1990 1992 1 3 0 0 433 1994 1997 1 5 0 0 433 1992 1994 2 -8 0 0 433 2000 2004 2 -8 2 0 433 1997 2000 3 -8 -6 0 433 2004 2006 3 -8 -6 3 Regards, Rebecca On Mon, Jun 10, 2013 at 3:51 PM, Kai Huang <demonsecret@hotmail.com.hk> wrote: > 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: rebecca.a.pope@gmail.com >> Date: Mon, 10 Jun 2013 08:55:25 -0500 >> Subject: Re: st: Calculating span exculsive of the current spell >> To: statalist@hsphsun2.harvard.edu >> >> 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 <demonsecret@hotmail.com.hk> 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/ * * 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/

**References**:**st: Calculating span exculsive of the current spell***From:*Kai Huang <demonsecret@hotmail.com.hk>

**Re: st: Calculating span exculsive of the current spell***From:*Rebecca Pope <rebecca.a.pope@gmail.com>

**RE: st: Calculating span exculsive of the current spell***From:*Kai Huang <demonsecret@hotmail.com.hk>

- Prev by Date:
**Re: st: including parentheses in stata helpfile** - Next by Date:
**st: Problem with datetime functions** - Previous by thread:
**RE: st: Calculating span exculsive of the current spell** - Next by thread:
**st: post-estimation with MI data** - Index(es):