Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: grouping by time window


From   "Svend Juul" <SJ@SOCI.AU.DK>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: grouping by time window
Date   Mon, 13 Jun 2005 22:58:44 +0200

André,
 
Although - as Nick wrote - the utility may not be quite clear 
to others than yourself, the problem is a bit fascinating. 
Find below a do-file that seems to give the answer. Comments 
are not as elaborate as they should be, but copy it and run it.
I simplified things by letting Firm 1,A and 1,B just have 
different firm codes.
 
Hope this helps
Svend
 
 
-------------------------------------------
clear
input firm str9 sdate
1 31dec2002
1 02apr2003
1 12dec2003
1 05jan2004
1 04may2004
2 04may2004
3 01jan2001
4 31dec2002
4 02apr2003
4 12dec2003
4 05jan2004
4 04may2004
4 01feb2005
4 01may2005
5 01jan2001
5 04may2004
end
 
gen date=date(sdate,"dmy")
format date %d
drop sdate
 
* generate firms1a.dta (all potential observations)
* and firms.dta (no observations - for appending)
sort firm date
gen obs=_n
by firm: gen prodno=_n
by firm: gen totprod=_N
by firm: gen lagtime=date-date[1]
drop if totprod<2
save firms1a.dta , replace
drop if _n>0
save firms.dta , replace
 
* Up to 10 groups per firm
forvalues I = 1/10 {

  * save valid groups
  use firms`I'a.dta , clear
  keep if lagtime<366
  sort firm date
  gen firmgroup=`I'
  by firm: replace totprod=_N
  drop if totprod<2
  local N = _N
  if `N' == 0 {
    continue , break
  }
  append using firms.dta
  save firms.dta , replace
 
  * save all unused potential observations
  use firms`I'a.dta , clear
  keep if lagtime>365
  sort firm date
  by firm: replace totprod=_N
  by firm: replace lagtime=date-date[1]
  drop if totprod<2
  local J = `I'+1
  save firms`J'a.dta , replace
}
 
use firms.dta , clear
sort firm date
list , sepby(firmgroup)
-------------------------------------------
 
________________________________________________________ 
 
Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6 
DK-8000 Aarhus C,  Denmark 
Phone, work:  +45 8942 6090 
Phone, home:  +45 8693 7796 
Fax:          +45 8613 1580 
E-mail:       sj@soci.au.dk 
_________________________________________________________ 

*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index