Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: on data transformation

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: on data transformation Date Wed, 20 Jul 2011 15:44:36 -0500

```Much depends on whether years with the event can only occur once for
each object.

Let's assume not to make the problem more challenging. Here is one way
to do it:

gen tag = occurred
levelsof x, local(X)

foreach x of local X {
levelsof year if x == `x' & occurred, local(Y)
foreach y of local Y {
replace tag = 1 if year == `y' - 1 & x == `x'
}
}

keep if tag

On Wed, Jul 20, 2011 at 3:21 PM,  <rado645-bg@yahoo.de> wrote:
> Dear Nick,
>
> thanks a lot! This was very helpful. Indeed, the event variable takes the value
> of 1 at the time of event and zero before that.
>
>
> I have just a quick question: how shall I proceed if I need to keep the year
> when the event has occurred and the previous year but drop any other preceding
> years?
>
> KR,
>
>
>
> ----- Ursprüngliche Mail ----
> Von: Nick Cox <njcoxstata@gmail.com>
> An: statalist@hsphsun2.harvard.edu
> Gesendet: Mittwoch, den 20. Juli 2011, 21:49:22 Uhr
> Betreff: Re: st: on data transformation
>
> It is not clear what value -event- takes when it is not 1. I will suppose 0.
>
> bysort x year (event) : gen occurred = event[_N]
>
> spreads occurrences to all the weeks in the year in which they occurred.
>
> keep if occurred
>
> then -drops- entire years without events.
>
> Nick
>
> On Wed, Jul 20, 2011 at 2:41 PM,  <rado645-bg@yahoo.de> wrote:
>>
>> I would like to ask for help with data transformation. I have data on objects
>> x1, x2,x3  that are observed over time (weeks). I have variables var1 and var2
>> for each objects and also variable indicating the year of observation. In
>> addition, a variable "event" indicates the time period when an event has
>> occurred.
>>
>>
>> The columns of the panel data set look as follows:
>>
>> x  week      var1  var2 year event
>>
>> x1 2006w2 var1 var2 2006
>> x1 2006w3 var1 var2 2006
>> x1 2006w4 var1 var2 2006
>> x1 2007w7 var1 var2 2007
>> x1 2007w8 var1 var2 2007
>> x1 2007w9 var1 var2 2007
>> x1 2007w10 var1 var2 2007 1
>>
>> x2 2008w2 var1 var2 2008
>> x2 2008w3 var1 var2 2008
>> x2 2008w4 var1 var2 2008
>> x2 2009w9 var1 var2 2009
>> x2 2009w8 var1 var2 2009
>> x2 2009w9 var1 var2 2009
>> x2 2009w10 var1 var2 2009 1
>>
>> I have many variables x that differ in number of weeks and years. I need
>>however
>> to keep the year when the event has happened and dropped any previous years.
>> Given my data set, is this possible to be done in Stata?
>>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```