Bookmark and Share

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]

Re: st: Start/End dates to ongoing count indicators


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Start/End dates to ongoing count indicators
Date   Tue, 23 Apr 2013 17:18:19 +0100

I assume numeric variables with formats showing Stata weekly dates.

First you need an observation identifier

gen long obsid = _n

and then a duration count

gen duration = EndDt - StDt  + 1

and then you want one observation per week

expand duration

Tidy up the dates

bysort obsid : gen Week = StDt[1] + _n - 1

Then

contract Week Class

and finally it's a -reshape-.

Nick
njcoxstata@gmail.com


On 23 April 2013 17:04, Jeremy Wells <jwell33@tigers.lsu.edu> wrote:

> I have a dataset where observations are unique projects. Each project
> has a start week and end week variable, as well as a classification variable,
> so the data structure looks like this:
>
> StDt                    EndDt           Class
> 2002wk1         2002 wk3                1
> 2002wk4         2002wk6         1
> 2002wk1         2002wk5         2
> 2002wk4         2002wk5         2
>
> What I would like to have is time series data with each observation a week
> and variables for each class with a count of the number of ongoing projects
> in each class in each week, so the data would look like this:
>
> Week            Class1          Class2
> 2002 wk1                1                       1
> 2002wk2         1                       1
> 2002wk3         1                       1
> 2002wk4         1                       2
> 2002wk5         1                       2
> 2002wk6         1                       0
>
> The first though is -tab class, gen(class)-, but that will give me a 1 for the
> start week only, but I want the count to continue to the end week.
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index