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/