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: on data transformation


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: on data transformation
Date   Mon, 25 Jul 2011 16:17:14 +0100

Yes indeed. This x is, more generally, a variable. The choice of x
just echoes the example you gave yourself in the first posting.

Nick

On Mon, Jul 25, 2011 at 4:05 PM,  <[email protected]> wrote:
> Dear Nick,
>
> I run the code but I always get the following error message after command line:
> levelsof x, local(X)
>
>
> . g tag = occurred
> (5356 missing values generated)
>
> . levelsof x, local(X)
> variable x not found
>
> That is why I was wondering if x shall be a variable from my dataset. Do you
> think I shall add additional command line about the macros?
>
>
> R.
>
>
>
> ----- Ursprüngliche Mail ----
> Von: Nick Cox <[email protected]>
> An: [email protected]
> Gesendet: Montag, den 25. Juli 2011, 15:44:52 Uhr
> Betreff: Re: st: on data transformation
>
> There is no need to run this via an .ado file. I can't see why that
> question arises.
>
> x and y are just used on the fly as macros within the loop. You don't
> need to use those names if you want to use other names.
>
> On Mon, Jul 25, 2011 at 2:25 PM,  <[email protected]> wrote:
>> Hi Nick,
>>
>> I have just had a look at your code. Thanks a lot for your feedback. I am
>>trying
>> to run the command but I am stuck with the loop: Could you please help me with
>> that:
>>
>>
>> I have the following code:
>>
>> //here I create the "occurred" variable
>>
>> bysort id year (failed) : gen occurred = failed[_N]
>> *keep if occurred
>>
>> //leave observations in the year of failure and the previous one (I follow the
>> code from your last message
>>
>> replace 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
>>
>>
>> I wonder if I shall replace x and y with other variables here.Or shall I run
>>the
>> loop part as an ado file? In such case, what shall x and y be?
>>
>>
>> Many thanks in advance.
>>
>> Best,
>> Rado
>>
>>
>>
>>
>> ----- Ursprüngliche Mail ----
>> Von: Nick Cox <[email protected]>
>> An: [email protected]
>> Gesendet: Mittwoch, den 20. Juli 2011, 22:44:36 Uhr
>> Betreff: Re: st: on data transformation
>>
>> 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,  <[email protected]> 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,
>>> Rado
>>>
>>>
>>>
>>> ----- Ursprüngliche Mail ----
>>> Von: Nick Cox <[email protected]>
>>> An: [email protected]
>>> 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,  <[email protected]> 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/
>
>
> *
> *   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/
>

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index