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: what command to keep firms-years with multiple occurrence of only one type of events


From   Aljar Meesters <[email protected]>
To   [email protected]
Subject   Re: st: what command to keep firms-years with multiple occurrence of only one type of events
Date   Fri, 27 Sep 2013 13:21:49 +0200

Dear Nahla,

I have encountered a similar problem as you and I couldn't find a
standard solution either, so I have written a little program that does
the trick. The program counts the number of unique values in a
variable and can be used with by. Mind you that, although the program
works for me, it hasn't been tested properly, yet it seems to work on
your use case as well.
Save the following code as _gunique.ado in a place where Stata can
find it (e.g. in your personal ado directory, which you can find by
typing "disp c(sysdir_personal)" ).

*** _gunique.ado ***
program define _gunique
syntax newvarname =/exp [if] [in] [, BY(varlist)]
    tempvar touse x
    qui{
    gen double `x' = `exp'
    gen byte `touse' = 1 `if' `in'
    sort `touse' `by' `x'

    by `touse' `by' (`x') : gen `varlist' = _n == 1
    by `touse' `by' (`x') : replace `varlist' = `varlist'[_n - 1]  +
(`x' != `x'[_n - 1]) if _n != 1
    by `touse' `by' (`x') : replace `varlist' = `varlist'[_N]
    replace `varlist' = . if `touse' != 1
    }
end

*** end file ***

Now you can use:
bysort Firm Year : egen foo = unique(Event)
keep if foo == 1 & Event == 3

which gives you the desired result.
Best,

Aljar

2013/9/27 Nahla Betelmal <[email protected]>:
> Dear Statalist members,
>
> It would be great if you can let me know what command I can use to
> keep firms-years with multiple occurrence of only one type of events
> in my data.
>
> I have firms-years-events data set. A firm can have multiple entries
> for the same year if it undertakes certain corporate events . I coded
> the events as 1,2,3, 4. For example the data looks like this
>
>
> Firm   Year    Event
> A        1994       2
> A        1994       3
> A        1994       3
> A        1995       1
> A         1995      3
> A         1996       2
> A         1996       4
> A         1996       3
> A          1997       3
> A           1997      3
> A           1998       1
> A            1998       1
> B
> ..
>
>
> I am only interested in studying firm-years where  only event 3 was
> undertaken (i.e. keep both entries for year 1997 only). In other
> words, I want to drop firm years where there are a mix of events shown
> as multiple entries/observations ( years 1994, 1995, 1996) and
> multiple unique occurrence of event other than 3 (year 1998).
>
> I understand that I can do it on two stages, first drop  firm-years
> with mix events, then second stage drop years with unique events other
> than 3.
>
> the problem that I  don't know what command should I use to do the
> first stage ( drop firms-years with multiple entries of multiple
> events).
>
> Thank you in advance, I highly appreciate your kind help
>
> Nahla Betelmal
> *
> *   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/


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