ch12.5.do open
file:script:do
| # | content | 
|---|---|
| 1 [+] | * ITSP 12.5 - | 
| 3 [+] | program drop _all | 
 /* *! onespell 1.1.1  CFBaum  13jan2005 * locate units with internal gaps in varlist and zap all but longest spell program onespell, rclass version 8.2 syntax varlist(numeric) [if] [in], Saving(string) [ REPLACE NOIsily] preserve quietly tsset local pv  "`r(panelvar)'" local tv "`r(timevar)'" summarize `pv', meanonly local n1 = r(N) tsfill marksample touse tempvar testgap spell seq end maxspell keepspell wantspell  | 
|
| 49 [+] | webuse grunfeld , | 
| 50 [+] | quietly replace invest = . in 28 | 
| 51 [+] | quietly replace mvalue = . in 55 | 
| 52 [+] | quietly replace kstock = . in 87 | 
| 53 [+] | quietly replace kstock = . in 94 | 
| 54 [+] | onespell invest mvalue kstock , | 
| 56 [+] | use grun1 , | 
| 57 [+] | tab company | 
* ITSP 12.5 -
program drop _all
/*
*! onespell 1.1.1  CFBaum  13jan2005
* locate units with internal gaps in varlist and zap all but longest spell
program onespell, rclass
	version 8.2
	syntax varlist(numeric) [if] [in], Saving(string) [ REPLACE NOIsily]
	preserve
	quietly tsset
	local pv  "`r(panelvar)'"
	local tv "`r(timevar)'"
	summarize `pv', meanonly
	local n1 = r(N)
	tsfill
	marksample touse
	tempvar testgap spell seq end maxspell keepspell wantspell
	local sss "quietly"
	if "`noisily'" == "noisily" {
        local sss "noisily"
     }
	`sss' {
* testgap is panelvar if obs is usable, 0 otherwise	
	generate `testgap' = cond(`touse',`pv',.)
	tsspell `testgap' if `testgap'<., spell(`spell') seq(`seq') end(`end')
	drop if `spell'==0 | `touse'==0
* if `spell' > 1 for a unit, there are gaps in usable data
* calculate max length spell for each unit and identify
* that spell as the one to be retained
	egen `maxspell'= max(`seq'), by(`pv')
	bysort `pv': generate `keepspell' = cond(`seq'==`maxspell',`spell',0)
	egen `wantspell' = max(`keepspell'), by(`pv')
* in case of ties, latest spell of max length is selected
	list `pv' `tv' `spell' `seq' `maxspell' `keepspell' `wantspell', sepby(`pv') 
	summarize `spell' `wantspell'
	keep if `wantspell' == `spell'
	summarize `pv', meanonly
	local n2 = r(N)
	drop __*
	}
	display _n "Observations removed: " `n1'-`n2'
	save `saving',`replace'
	restore
end
*/
webuse grunfeld, clear
quietly replace invest = . in 28
quietly replace mvalue = . in 55
quietly replace kstock = . in 87
quietly replace kstock = . in 94
onespell invest mvalue kstock, saving(grun1) replace
use grun1, clear
tab company