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: panel data dummy creation/incorporation
From
Andrew Reed <[email protected]>
To
[email protected]
Subject
Re: st: panel data dummy creation/incorporation
Date
Mon, 19 Aug 2013 14:18:03 +0200
Hi Nick,
I've run the code you provided and weirdly enough an unrecognized command error pops up in relation to the summarize command.
It is listed as an invalid command name but when just checking if su works outside of the code, as anticipated, it works without any problem. Should something within this code be changed?
Andrew
Den 19/08/2013 kl. 13.45 skrev Nick Cox:
> Your specification refers at one point to "every" other entity
> experiencing an event, but your use of -collapse (max)- and the rest
> of your discussion implies _any_ other event. (Every would imply
> looking at minima.)
>
> I've referred you twice previously to
>
> http://www.stata.com/support/faqs/data-management/creating-variables-recording-properties/
>
> and your problem falls under that heading in requiring a loop over
> identifiers to look in turn at each identifier compared with all the
> others.
>
> As you want to work separately for each date, your problem further
> separates into a loop over all observations, if I understand it
> correctly.
>
> With your sample data try this
>
> gen newevent2 = .
>
> quietly forval i = 1/`=_N' {
> su event if time == time[`i'] & id != id[`i'], meanonly
> replace newevent2 = r(max) in `i'
> }
>
> assert newevent2 == newevent
>
> On your mention of a "summation function", I can only repeat what I
> said earlier:
>
> "There is a -summarize- command (which can be abbreviated -sum-), a
> -sum()- function and an -egen- function -sum()-, so it is not clear
> which sandpit you're playing in."
>
> Nick
> [email protected]
>
> On 19 August 2013 11:39, Andrew Reed <[email protected]> wrote:
>
>> I have been struggling with this for a few days and the closest I've come to finding a decent answer is by way of the collapse command.
>>
>> I have 79 different entities in a panel data set, where each entity has its own specific set of information concerning. The dummy variable takes on a value of 1 if there has occurred an event (credit rating movement) for that particular id/entity.
>>
>> What I want to do from this point is to create, for each entity again, a new dummy variable where the value of 1 signifies an event occurring to every entity but the entity itself that i am creating the dummy for. As mentioned before, the collapse command gives me the exact output i'd like, collapsing the dummy variable by date across all id's. This also gives me the option to exclude the id value I am creating the dummy for. The code is as follows.
>>
>> collapse (max) event_variable if id!=(id value), by date
>>
>> The major problem is that I have to copy/paste this input back into the dataset and go through each id value individually.
>>
>> I'm therefore trying to incorporate a foreach statement in order to have this same type of operation go through the data set and automatically create the measure I want for each id value. I'm still a bit confused as all of the reading i've seen is using the summation function and not quite the collapse function that has worked to give me exactly what I wanted, just in a not-so-elegant way.
>>
>> I would gladly accept generating a new variable where, for each id value, the dummy values for all other id values are added, and from this point i could just replace all values that are not 0 with 1. I have included a visual representation of what I want to do below, where newevent is the new dummy.
>>
>> id event time newevent
>> 1 0 1 1
>> 1 1 2 0
>> 1 0 3 1
>> 1 0 4 0
>> 1 0 5 1
>> 2 1 1 0
>> 2 0 2 1
>> 2 1 3 0
>> 2 0 4 0
>> 2 0 5 1
>> 3 0 1 1
>> 3 0 2 1
>> 3 0 3 1
>> 3 0 4 0
>> 3 1 5 0
>>
>> I hope this makes sense and someone may have something using 'foreach' or 'by id, sort' that can help me in doing this.
>
> *
> * 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/