1 /* Test file for kate's stata syntax highlighting
3 *! version 1.2.0 2mar2003 E. Leuven
4 program define spellsplit
6 syntax [anything], spell(varlist min=2 max=2) [ by(varlist)]
10 local f0 : format `date0'
11 local f1 : format `date1'
13 /* set default statistic */
16 gettoken left anything : anything, match(prns)
19 if !inlist("`left'","mean","sum") {
20 di as error "Statistic `left' not supported"
23 local current "`left'"
26 local `current'vars ``current'vars' `left'
28 gettoken left anything : anything, match(prns)
30 if ("`meanvars'"!="") {
31 confirm var `meanvars'
32 unab meanvars : `meanvars'
34 if ("`sumvars'"!="") {
36 unab sumvars : `sumvars'
41 local xvars `meanvars' `sumvars' _count
43 /* create dummy by-var if no by option is specified */
50 /* create negative for subtraction when spell ends */
51 cap foreach v of varlist `xvars' {
53 local txvars `txvars' ``v''
55 cap foreach v of varlist `by' {
57 local txvars `txvars' ``v''
60 stack `date0' `xvars' `by' `date1' `txvars', into(`date0' `xvars' `by') clear
62 /* calculate totals per date */
63 cap foreach v of varlist `xvars' {
64 egen double ``v'' = sum(`v'), by(`by' `date0')
67 /* unique dates only */
68 by `by' `date0', sort: keep if _n==1
70 /* calculate totals (+ when spell starts - when ends) */
72 cap foreach v of varlist `xvars' {
73 by `by': replace `v' = sum(``v'')
75 by `by': g `date1' = `date0'[_n + 1]
77 drop if `date0'>`date1'
80 order `by' `date0' `date1' `xvars'
84 cap for var `meanvars': replace X = X/_count