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]

Fwd: Re: st: Obtain information from nearest observation


From   Chamara Anuranga <[email protected]>
To   [email protected]
Subject   Fwd: Re: st: Obtain information from nearest observation
Date   Tue, 11 Sep 2012 16:18:36 +0530

someone may need similar code. So I am forwarding this to maillist


---------- Forwarded message ----------
From: "Marco Müller" <[email protected]>
Date: Tue, Sep 11, 2012 at 2:56 PM
Subject: Re: Re: st: Obtain information from nearest observation
To: Chamara Anuranga <[email protected]>


Hey
Wooow, impressive, it works. Thank you very much for you help.
You made my day ;)

-------- Original-Nachricht --------
> Datum: Tue, 11 Sep 2012 14:39:36 +0530
> Von: Chamara Anuranga <[email protected]>
> An: "Marco Müller" <[email protected]>
> Betreff: Re: Re: st: Obtain information from nearest observation

> Hi,
>
> Try this.
>
>
> use "dataseta.dta",clear
> append using "datasetb.dta"
> gen data=1 if event_date~=.
> replace data=2 if data==.
> replace date=event_date if event_date!=.
> sort company date
> gen proevent=net_sales
> gen prodate=date if net_sales~=.
> *Before the event
> bys company: replace proevent=proevent[_n-1] if proevent==.
> bys company:  replace prodate=prodate[_n-1] if prodate==.
>
> *After the event
>
> gen aftevent=net_sales
> gen aftdate=date if net_sales~=.
>  gsort company -date
> bys company:replace aftevent=aftevent[_n-1] if aftevent==.
> bys company:replace aftdate=aftdate[_n-1] if aftdate==.
> keep if data==1
> drop net_sales
> format prodate aftdate   %tdDD/NN/CCYY
>
>
>
> On Tue, Sep 11, 2012 at 2:00 PM, "Marco Müller" <[email protected]> wrote:
> > Hi Chamara
> > Thanks for your help. Unfortunately I could not figure out how to
> implement the [_n-1] (I am a novice to stata). I tried to tsset the dates first.
> But, since the dates are similar for many companies, tsset does not work.
> In addition, by company: tsset is not allowed.
> > I added sample data in the attachement. Could you help me implementing
> your code?
> > Many many thanks.
> >
> > Best regards,
> > Marco
> >
> > -------- Original-Nachricht --------
> > Datum: Fri, 7 Sep 2012 22:22:59 +0530
> > Von: Chamara Anuranga <[email protected]>
> > An: [email protected]
> > Betreff: Re: st: Obtain information from nearest observation
> >
> > Hi,
> >
> > Append two data set. put date variables together. sort by company and
> > date. Then fill the blank for net sales variable taking previous
> > value.
> > replace net_sales=net_sates[_n-1] if net_sales==.
> > keep the dataset if event_date not missing.
> >
> > Hope this help. If you need more information please add sample data.
> >
> > Thanks
> > Chamara
> >
> > On Fri, Sep 7, 2012 at 9:24 PM, "Marco Müller" <[email protected]> wrote:
> >> Hi all
> >>
> >> I have the following problem: I have a dataset A with event dates for
> >> firms as:
> >> COMPANY    EVENT_DATE
> >> 1          01/03/1986
> >> 2          15/08/2001
> >>
> >>
> >> and I have a dataset B with financial information about the firms,
> e.g.:
> >> COMPANY     DATE             NET_SALES
> >> 1           31/12/1985       1'000
> >> 1           31/12/1986       2'000
> >> 1           31/12/1987       1'200
> >> ....
> >> 2           30/06/2000         400
> >> 2           30/06/2001         600
> >> 2           30/06/2002         700
> >>
> >> I'd like to obtain the last available net sales information PRIOR to
> the
> >> event date (or, in another case, the first one FOLLOWING the event
> date).
> >> For example, I'd like to obtain the net sales of 1'000 and 600 for A
> and B
> >> (prior to the events) and 2'000&700 following the events.
> >>
> >> Any suggenstions how to handle that?
> >>
> >> Many thanks for your suggestions
> >>
> >> Best regards,
> >> Marco
> >> *
> >> *   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