Thank you very much, Nick,
That is exactly what I need, except that I want to exclude the same
day's transactions (so I only need to change the 0 in the inrange
function to 1).
It is a little bit slow. But since I have only around 21000
observations, it did not take too long.
Again, thank you.
Best
Gao
On 10/10/07, Nick Cox <[email protected]> wrote:
> This came up earlier this year on Statalist and was the subject
> of a Tip in the Stata Journal:
>
> see e.g.
>
> <http://www.stata.com/statalist/archive/2007-07/msg00673.html>
>
> earlier and later posts in the same thread
>
> and Stata Tip 51 "Events in intervals" in SJ 7(3) 2007.
>
> I am going to assume that 730 days earlier is adequate
> as a definition of two years previous. If you want something
> more elaborate, watch out for
> a problem with leap years: if 29 Feb 2000 (say) was a transaction
> date then
>
> mdy(month(sale_date), day(sale_date), year(sale_date) - 2)
>
> will yield missing as there was no 29 Feb 1998. Thus
> you might want instead
>
> cond(month(sale_date) == 2 & day(sale_date) == 29,
> mdy(3, 1, year(sale_date)),
> mdy(month(sale_date), day(sale_date), year(sale_date) - 2))
>
> In essence, I think you need to loop over observations:
>
> gen trans_prev2yr = .
>
> qui forval i = 1/`=_N' {
> count if bond == bond[`i'] & inrange(sale_date[`i'] - sale_date, 0, 730)
> replace trans_prev2yr = r(N) in `i'
> }
>
> This will be slow, but is, modulo any typos, a solution.
>
> Nick
> [email protected]
>
> Gao Liu
>
> > I have a dataset that contains three variables: transaction_ID,
> > bond_issuer_name and sale_date. Each issuer may have multiple
> > transactions in the dataset. I want to creat a new variable indicating
> > that for each transaction, how many previous transactions the same
> > bond issuer have in the past two years (two years before the sale day
> > of the transaction).
>
> *
> * For searches and help try:
> * http://www.stata.com/support/faqs/res/findit.html
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
--
#405 Patterson Office Tower
Martin School of Public Administration and Policy
University of Kentucky
Phone: 859-257-5481
Fax: 859-323-1937
[email protected]
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/