*
* @ipshin( options ) series start end
*
* Implements tests from Im, Pesaran and Shin, "Testing for Unit Roots in
* Heterogeneous Panels", J. of Econometrics, 2003, vol 115, pp 53-74.
* This aggregates the ADF test statistics computed separately across
* individuals. Under the null, the average ADF statistic has a Normal
* limiting distribution as N->infinity.
*
* The two main test statistics are z(tbar) and z(ttildebar), which are
* the standardized averages of the standard ADF t-test (z(tbar) and the
* ADF t-test with variance computed under the unit root hypothesis).
*
* Requires version 7.30 or later of RATS due to the TITLE option on
* REPORT(ACTION=DEFINE). That just affects the title of the window if
* you reload it with the Window--Restore Report operation. If you remove
* that, it will work with 7.1 or later.
*
* Parameters:
*
*   series     = series to analyze
*   start end  = range to use in performing tests [by default, maximum range of series]
*
* Options:
*   DET=NONE/[CONSTANT]/TREND (NONE is an error)
*   LAGS=(maximum number of) augmenting lags in the ADF regressions [Schwert's]
*   CRIT=[FIXED]/GTOS/AIC/BIC/HQ
*     Criterion to use to select the number of lags. This is done on an
*     individual by individual basis. CRIT=FIXED uses the input LAGS on
*     each. CRIT=GTOS starts with LAGS and drops lags until it hits one
*     which has the marginal t with significance level set by the SLSTAY
*     option. CRIT=AIC/BIC/HQ select the lag length using AIC, BIC (or SBC)
*     or HQ.
*   SLSTAY=significance level to keep lag in model with METHOD=GTOS [.10]
*
*   SMPL=Dummy series with 0's in entries to skip [include all]
*
*   TITLE=title for output ["Im-Pesaran-Shin Unit Root Test: Series... "]
*   [PRINT]/NOPRINT
*
option choice  det    2 none constant trend
option integer lags
option choice  crit   1  fixed gtos aic bic hq
option real    slstay .10
option series  smpl
option switch  print  1
option string  title
option integer block
*
* Variables Defined:
*   %CDSTAT - the "zbar" (standardized Normal) statistic
*   %TBAR   - the average DF statistic
*   %NGROUP - number of individuals
*   %%ILAGS - lags used in individual analysis
*
* Revision Schedule:
*  09/2004 Written by Tom Doan, Estima
*  07/2007 Correct significance level for positive z statistics
*  03/2009 Minor change to formatting
*  07/2010 Switch to use REPORT for output. Add TITLE option. Add DET option
*          as more standard treatment. Get rid of BLOCK option.
*  06/2012 Add %NGROUP and %%ILAGS as defined globals.
*
procedure ipshin y start end
type series 	y
type integer 	start end
*
option choice  det    2  none constant trend
option integer lags   0
option choice  crit   1  fixed gtos aic bic hq
option real    slstay .10
option series  smpl
option switch  print  1
option string  title
option integer block
*
decl  vect[integer] %%ilags
*
local integer 	startl endl starti endi
local integer	i j k p pmax tmax tend mint maxt slot
local series 	time dy icount
local vect[int] detvars lagsused
local real 		tt ttbar ttildebar pt et etilde vt vtilde nt marginal_t logl ic bestic
local real 		avglags adjust ttmu ttvar ttildemu ttildevar ttilde zbar ztildebar
local real 		cv01 cv05 cv10
local string   ltitle descript
local report   ipreport
local rect     sweepxx
fixed vect[string] lagcriterion(5)
Fixed
General-to-specific
AIC
BIC/SBC
Hannan-Quinn
*
* Lookup table sizes
*
fixed vect[int] tmomsizes(15)
 6 7 8 9 10 15 20 25 30 40 50 100 500 1000 1000000
*
* Moments (mean in row 1, variance in row 2) for t (standard ADF t's on
* the lagged y) and ttilde (t with sigma squared computed under the
* null) statistics.
*
fixed rect ttildemoments(2,15)
 -1.125 -1.178 -1.214 -1.244 -1.274 -1.349 -1.395 -1.423 -1.439 -1.463 -1.477 -1.504 -1.526 -1.526 -1.533
  0.497  0.506  0.506  0.527  0.521  0.565  0.592  0.609  0.623  0.639  0.656  0.683  0.704  0.702  0.706
fixed rect ttmoments(2,15)
 -1.520 -1.514 -1.501 -1.501 -1.504 -1.514 -1.522 -1.520 -1.526 -1.523 -1.527 -1.532 -1.531 -1.529 -1.533
  1.745  1.414  1.228  1.131  1.069  0.923  0.851  0.809  0.789  0.770  0.760  0.735  0.715  0.707  0.706
*
* Lookup tables for critical values of the average t statistics. 1%, 5%
* and 10% c.v's for different combinations of N and T.
*
fixed vect[int] dftsizes(12)
  5 10 15 20 25 30 40 50 60 70 100 1000000
fixed vect[int] dfnsizes(8)
  5  7 10 15 20 25 50 100
fixed vect[rect] dfcv_constant(3)(8,12)
-3.79, -2.66, -2.54, -2.50, -2.46, -2.44, -2.43, -2.42, -2.42, -2.40, -2.40, -2.40
-3.45, -2.47, -2.38, -2.33, -2.32, -2.31, -2.29, -2.28, -2.28, -2.28, -2.27, -2.27
-3.06, -2.32, -2.24, -2.21, -2.19, -2.18, -2.16, -2.16, -2.16, -2.16, -2.15, -2.15
-2.79, -2.14, -2.10, -2.08, -2.07, -2.05, -2.04, -2.05, -2.04, -2.04, -2.04, -2.04
-2.61, -2.06, -2.02, -2.00, -1.99, -1.99, -1.98, -1.98, -1.98, -1.97, -1.97, -1.97
-2.51, -2.01, -1.97, -1.95, -1.94, -1.94, -1.93, -1.93, -1.93, -1.93, -1.92, -1.92
-2.20, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-2.00, -1.75, -1.74, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73

-2.76, -2.28, -2.21, -2.19, -2.18, -2.16, -2.16, -2.15, -2.16, -2.15, -2.15, -2.15
-2.57, -2.17, -2.11, -2.09, -2.08, -2.07, -2.07, -2.06, -2.06, -2.06, -2.05, -2.05
-2.42, -2.06, -2.02, -1.99, -1.99, -1.99, -1.98, -1.98, -1.97, -1.98, -1.97, -1.97
-2.28, -1.95, -1.92, -1.91, -1.90, -1.90, -1.90, -1.89, -1.89, -1.89, -1.89, -1.89
-2.18, -1.89, -1.87, -1.86, -1.85, -1.85, -1.85, -1.85, -1.84, -1.84, -1.84, -1.84
-2.11, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-1.95, -1.75, -1.74, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73, -1.73
-1.84, -1.68, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67, -1.67

-2.38, -2.10, -2.06, -2.04, -2.04, -2.02, -2.02, -2.02, -2.02, -2.02, -2.01, -2.01
-2.27, -2.01, -1.98, -1.96, -1.95, -1.95, -1.95, -1.95, -1.94, -1.95, -1.94, -1.94
-2.17, -1.93, -1.90, -1.89, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88, -1.88
-2.06, -1.85, -1.83, -1.82, -1.82, -1.82, -1.81, -1.81, -1.81, -1.81, -1.81, -1.81
-2.00, -1.80, -1.79, -1.78, -1.78, -1.78, -1.78, -1.78, -1.78, -1.77, -1.77, -1.77
-1.96, -1.77, -1.76, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75, -1.75
-1.85, -1.70, -1.69, -1.69, -1.69, -1.69, -1.68, -1.68, -1.68, -1.68, -1.69, -1.69
-1.77, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64, -1.64

fixed vect[rect] dfcv_trend(3)(8,11)
-8.12, -3.42, -3.21, -3.13, -3.09, -3.05, -3.03, -3.02, -3.00, -3.00, -2.99, -2.99
-7.36, -3.20, -3.03, -2.97, -2.94, -2.93, -2.90, -2.99, -2.88, -2.87, -2.86, -2.86
-6.44, -3.03, -2.88, -2.84, -2.82, -2.79, -2.78, -2.77, -2.76, -2.75, -2.75, -2.75
-5.72, -2.86, -2.74, -2.71, -2.69, -2.68, -2.67, -2.65, -2.66, -2.65, -2.64, -2.64
-5.54, -2.75, -2.67, -2.63, -2.62, -2.61, -2.59, -2.60, -2.59, -2.58, -2.58, -2.58
-5.16, -2.69, -2.61, -2.58, -2.58, -2.56, -2.55, -2.55, -2.55, -2.54, -2.54, -2.54
-4.50, -2.53, -2.48, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.43, -2.43
-4.00, -2.42, -2.39, -2.38, -2.37, -2.37, -2.36, -2.36, -2.36, -2.36, -2.36, -2.36

-4.66, -2.98, -2.87, -2.82, -2.80, -2.79, -2.77, -2.76, -2.75, -2.75, -2.75, -2.75
-4.38, -2.85, -2.76, -2.72, -2.70, -2.69, -2.68, -2.67, -2.67, -2.66, -2.66, -2.66
-4.11, -2.74, -2.66, -2.63, -2.62, -2.60, -2.60, -2.59, -2.59, -2.58, -2.58, -2.58
-3.88, -2.63, -2.57, -2.55, -2.53, -2.53, -2.52, -2.52, -2.52, -2.51, -2.51, -2.51
-3.73, -2.56, -2.52, -2.49, -2.48, -2.48, -2.48, -2.47, -2.47, -2.46, -2.46, -2.46
-3.62, -2.52, -2.48, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.43, -2.43
-3.35, -2.42, -2.38, -2.38, -2.37, -2.37, -2.36, -2.36, -2.36, -2.36, -2.36, -2.36
-3.13, -2.34, -2.32, -2.32, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31, -2.31

-3.73, -2.77, -2.70, -2.67, -2.65, -2.64, -2.63, -2.63, -2.62, -2.63, -2.62, -2.62
-3.60, -2.68, -2.62, -2.59, -2.58, -2.57, -2.57, -2.56, -2.56, -2.55, -2.55, -2.55
-3.45, -2.59, -2.54, -2.52, -2.51, -2.51, -2.50, -2.50, -2.50, -2.49, -2.49, -2.49
-3.33, -2.52, -2.47, -2.46, -2.45, -2.45, -2.44, -2.44, -2.44, -2.44, -2.44, -2.44
-3.26, -2.47, -2.44, -2.42, -2.41, -2.41, -2.41, -2.40, -2.40, -2.40, -2.40, -2.40
-3.18, -2.44, -2.40, -2.39, -2.39, -2.38, -2.38, -2.38, -2.38, -2.38, -2.38, -2.38
-3.02, -2.36, -2.33, -2.33, -2.33, -2.32, -2.32, -2.32, -2.32, -2.32, -2.32, -2.32
-2.90, -2.30, -2.29, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28, -2.28
*
* Lookup tables for the mean and variance of the ADF statistics for
* combinations of ADF lags (down the columns, not across rows as
* presented in the paper) and time periods.
*
fixed vect[int] adftsizes(11)
  10 15 20 25 30 40 50 60 70 100 100000
fixed rect adfmu_constant(11,9)
-1.504, -1.488, -1.319, -1.306, -1.171,     NA,     NA,    NA,    NA
-1.514, -1.503, -1.387, -1.366, -1.260, -1.239,     NA,    NA,    NA
-1.522, -1.516, -1.428, -1.413, -1.329, -1.313, -1.238,    NA,    NA
-1.520, -1.514, -1.443, -1.433, -1.363, -1.351, -1.289, -1.273, -1.212
-1.526, -1.519, -1.460, -1.453, -1.394, -1.384, -1.331, -1.319, -1.266
-1.523, -1.520, -1.476, -1.471, -1.428, -1.421, -1.380, -1.371, -1.329
-1.527, -1.524, -1.493, -1.489, -1.454, -1.451, -1.418, -1.411, -1.377
-1.519, -1.519, -1.490, -1.486, -1.458, -1.454, -1.427, -1.423, -1.393
-1.524, -1.522, -1.498, -1.495, -1.470, -1.467, -1.444, -1.441, -1.415
-1.532, -1.530, -1.514, -1.512, -1.495, -1.494, -1.476, -1.474, -1.456
-1.532, -1.530, -1.514, -1.512, -1.495, -1.494, -1.476, -1.474, -1.456

fixed rect adfvar_constant(11,9)
1.069, 1.255, 1.421, 1.759, 2.080,    NA,    NA,    NA,    NA
0.923, 1.011, 1.078, 1.181, 1.279, 1.420,    NA,    NA,    NA
0.851, 0.915, 0.969, 1.037, 1.097, 1.171, 1.237,    NA,    NA
0.809, 0.861, 0.905, 0.952, 1.005, 1.055, 1.114, 1.164, 1.217
0.789, 0.831, 0.865, 0.907, 0.946, 0.980, 1.023, 1.062, 1.105
0.770, 0.803, 0.830, 0.858, 0.886, 0.912, 0.942, 0.968, 0.996
0.760, 0.781, 0.798, 0.819, 0.842, 0.863, 0.886, 0.910, 0.929
0.749, 0.770, 0.789, 0.802, 0.819, 0.839, 0.858, 0.875, 0.896
0.736, 0.753, 0.766, 0.782, 0.801, 0.814, 0.834, 0.851, 0.871
0.735, 0.745, 0.754, 0.761, 0.771, 0.781, 0.795, 0.806, 0.818
0.735, 0.745, 0.754, 0.761, 0.771, 0.781, 0.795, 0.806, 0.818

fixed rect adfmu_trend(11,9)
-2.166, -2.173, -1.914, -1.922, -1.750,     NA,     NA,     NA,     NA
-2.167, -2.169, -1.999, -1.977, -1.823, -1.804,     NA,     NA,     NA
-2.168, -2.172, -2.047, -2.032, -1.911, -1.888, -1.778,     NA,     NA
-2.167, -2.172, -2.074, -2.065, -1.968, -1.955, -1.868, -1.851, -1.761
-2.172, -2.173, -2.095, -2.091, -2.009, -1.998, -1.923, -1.912, -1.835
-2.173, -2.177, -2.120, -2.117, -2.057, -2.051, -1.995, -1.986, -1.925
-2.176, -2.180, -2.137, -2.137, -2.091, -2.087, -2.042, -2.036, -1.987
-2.174, -2.178, -2.143, -2.142, -2.103, -2.101, -2.065, -2.063, -2.024
-2.174, -2.176, -2.146, -2.146, -2.114, -2.111, -2.081, -2.079, -2.046
-2.177, -2.179, -2.158, -2.158, -2.135, -2.135, -2.113, -2.112, -2.088
-2.177, -2.179, -2.158, -2.158, -2.135, -2.135, -2.113, -2.112, -2.088

fixed rect adfvar_trend(11,9)
1.132, 1.453, 1.627, 2.482, 3.947,    NA,    NA,    NA,    NA
0.869, 0.975, 1.036, 1.214, 1.332, 1.590,    NA,    NA,    NA
0.763, 0.845, 0.882, 0.983, 1.052, 1.165, 1.243,    NA,    NA
0.713, 0.769, 0.796, 0.861, 0.913, 0.991, 1.055, 1.145, 1.208
0.690, 0.734, 0.756, 0.808, 0.845, 0.899, 0.945, 1.009, 1.063
0.655, 0.687, 0.702, 0.735, 0.759, 0.792, 0.828, 0.872, 0.902
0.633, 0.654, 0.661, 0.688, 0.705, 0.730, 0.753, 0.786, 0.808
0.621, 0.641, 0.653, 0.674, 0.685, 0.705, 0.725, 0.747, 0.766
0.610, 0.627, 0.634, 0.650, 0.662, 0.673, 0.689, 0.713, 0.728
0.597, 0.605, 0.613, 0.625, 0.629, 0.638, 0.650, 0.661, 0.670
0.597, 0.605, 0.613, 0.625, 0.629, 0.638, 0.650, 0.661, 0.670

*
* Check inputs
*
if .not.%defined(y) {
   disp "###Syntax: @IPSHIN( options ) >>>>>SERIES<<<<<"
   return
}
if %defined(block) {
   disp "###@IPSHIN(BLOCK=nnn). No longer supported. Use PANEL calendar scheme"
   return
}
if det==1 {
   disp "###@IPSHIN doesn't support DET=NONE"
   return
}
*
inquire(series=y) startl<<start endl<<end
clear dy
set dy startl+1 endl = y-y{1}
compute starti=%indiv(startl),endi=%indiv(endl)
panel(icount=1,smpl=smpl,compress) y startl endl icount
dim lagsused(endl)
sstats(max) starti endi icount>>pt
sstats      starti endi (icount>0)>>nt
compute tmax=fix(pt),tend=%panelobs()
*
* Count the number of individuals with data.
*
sstats(smpl=icount>0)  starti endi 1>>nt
*
* Set up the list of deterministic variables
*
if det==3 {
   set time = t
   compute detvars=||constant,time||
   compute descript="Constant + Trend"
}
else {
   compute detvars=||constant||
   compute descript="Constant"
}
*
* mint and maxt will keep track of the minimum and maximum number of
* time periods per individual. The other variables are for accumulation
* of means, variances and test statistics.
*
compute mint=maxt=0
compute ttbar=ttildebar=et=etilde=vt=vtilde=0.0
*
* Figure out lag lengths for individual specific ADF regressions. Use
* Schwert's rule of thumb if the maximum lag length wasn't given.
*
if .not.%defined(lags)
   compute pmax=fix(%round(12*(.01*tmax)^.25,0))
else
   compute pmax=lags
*
* Tables don't go beyond 8
*
compute pmax=%imin(pmax,8)
*
do i=starti,endi
   *
   * Skip anyone without data in the sample
   *
   if icount(i)==0
      next
   if crit==1 {
      compute lagsused(i)=pmax
      goto chosen
   }
   *
   * Set lagsused to 0, which will be the value if we don't find anything
   * significant at 1 or higher.
   *
   compute lagsused(i)=0
   cmom(smpl=smpl) i//(pmax+2) i//tend
   # dy y{1} detvars dy{1 to pmax}
   *
   * Start with the full regression
   *
   compute sweepxx=%sweeplist(%cmom,%seq(2,%ncmom))
   compute slot=%ncmom
   do p=pmax,(crit==2),-1
      compute %ndf=%nobs-(slot-1)
      if crit==2 {
         *
         * Compute the (absolute) t-statistic from the information in
         * <<sweepxx>>. The pivot is at <<slot>>, which is the number of
         * regressors + 1 for the dependent variable.
         *
         compute marginal_t=abs(sweepxx(slot,1))/$
           sqrt(sweepxx(slot,slot)*sweepxx(1,1)/%ndf)
         *
         * If that's significant at the requested level, break the loop.
         *
         if marginal_t>%invttest(slstay,%ndf) {
            compute lagsused(i)=p
            break
         }
      }
		else
		if crit==3.or.crit==4.or.crit==5 {
      compute logl=-.5*%nobs*log(sweepxx(1,1)/%nobs)
      if crit==3
         compute ic=2.0*logl-2*p
      else
      if crit==4
         compute ic=2.0*logl-log(%nobs)*p
      else
         compute ic=2.0*logl-2*log(log(%nobs))*p
      if p==pmax.or.ic>bestic
         compute bestic=ic,lagsused(i)=p
      }
      *
      * Sweep on the last lag to take it out of the regression.
      *
      compute sweepxx=%sweep(sweepxx,slot)
      compute slot=slot-1
   end do p
:chosen
   *
   * Run ADF regression over data for individual "i" using the full span
   * allowed by the chosen number of lags.
   *
   linreg(noprint,smpl=smpl) dy i//(lagsused(i)+2) i//tend
   # y{1} detvars dy{1 to lagsused(i)}
   *
   * tt is the standard t-statistic. adjust is the adjustment factor for
   * the variance and degrees of freedom for the variance calculated
   * under the null.
   *
   compute tt=%tstats(1)
   compute adjust=(tt^2/%ndf+1)*%ndf/(%ndf+1.0)
   compute ttilde=tt/sqrt(adjust)
   *
   * Adjust mint and maxt if necessary
   *
   if mint==0.or.%nobs<mint
      compute mint=%nobs
   if maxt==0.or.%nobs>maxt
      compute maxt=%nobs
   *
   * Look up the closest match (without going over the "T" dimension)
   * for the ADF mean and variance.
   *
   do j=1,%rows(adftsizes)
      if %nobs<=adftsizes(j).or.j==%rows(adftsizes) {
         compute ttmu =%if(det==3,adfmu_trend(j,pmax+1) ,adfmu_constant(j,pmax+1))
         compute ttvar=%if(det==3,adfvar_trend(j,pmax+1),adfvar_constant(j,pmax+1))
         compute ttildemu=ttmu,ttildevar=ttvar
         break
      }
   end do j
   *
   * For the special case of a simple DF regression without trend, use
   * the small (N dimension) sample lookup table instead.
   *
   if lags==0.and.%rows(detvars)==1 {
      do j=1,%rows(tmomsizes)
         if %nobs<=tmomsizes(j).or.j==%rows(tmomsizes) {
            compute ttildemu=ttildemoments(1,j),ttildevar=ttildemoments(2,j),ttmu=ttmoments(1,j),ttvar=ttmoments(2,j)
            break
         }
      end do j
   }

   compute ttbar=ttbar+tt,ttildebar=ttildebar+ttilde
   compute et   =et+ttmu ,etilde   =etilde+ttildemu
   compute vt   =vt+ttvar,vtilde   =vtilde+ttildevar
end do i
*
compute ttbar		=ttbar/nt
compute ttildebar	=ttildebar/nt
compute zbar		=nt*(ttbar-et/nt)/sqrt(vt)
compute ztildebar	=nt*(ttildebar-etilde/nt)/sqrt(vtilde)
*
sstats(smpl=icount>0,mean) starti endi lagsused(t)>>avglags
if %defined(title)
   compute ltitle=title
else
   compute ltitle="Im-Pesaran-Shin Unit Root Test: Series "+%l(y)

report(use=ipreport,action=define,title=ltitle)
report(use=ipreport,atrow=1,atcol=1,span) ltitle
report(use=ipreport,atrow=2,atcol=1,span) "Test has large N on large T asymptotics"
report(use=ipreport,atrow=3,atcol=1,span) "Null is Unit Root. Alternative is rho(i)<>1 for some i"
report(use=ipreport,atrow=4,atcol=1,span) "Individual Specific Components: "+descript
*
report(use=ipreport,atrow=6,atcol=1) "N" fix(nt)
report(use=ipreport,atrow=7,atcol=1) "T" fix(mint)
if maxt<>mint
   report(use=ipreport,atrow=7,atcol=3) "to" fix(maxt)
if crit==1
   report(use=ipreport,atrow=8,atcol=1) "P" pmax
else {
   report(use=ipreport,atrow=8,atcol=1) "Avg P" avglags
   report(use=ipreport,atrow=8,atcol=3,span) "chosen from "+pmax+" by "+lagcriterion(crit)
}
report(use=ipreport,atrow=9,atcol=2)               "Statistic" "Signif Level"
report(use=ipreport,atrow=10,atcol=1) "Z tbar "      zbar 		 %cdf(zbar)
report(use=ipreport,atrow=11,atcol=1) "Z ttildebar " ztildebar  %cdf(ztildebar)
*
* Only for balanced samples with p=0
*
if maxt==mint.and.pmax==0 {
  	do j=1,%rows(dftsizes)
     	if fix(maxt)<=dftsizes(j).or.j==%rows(dftsizes)
        	break
  	end do j
  	do k=1,%rows(dfnsizes)
     	if fix(nt)<=dfnsizes(k).or.k==%rows(dfnsizes)
        	break
  	end do k
  	if %rows(detvars)==2
     	compute cv01=dfcv_trend(1)(k,j),cv05=dfcv_trend(2)(k,j),cv10=dfcv_trend(3)(k,j)
  	else
     	compute cv01=dfcv_constant(1)(k,j),cv05=dfcv_constant(2)(k,j),cv10=dfcv_constant(3)(k,j)
	report(use=ipreport,atrow=12,atcol=1) "TBar" ttbar
	report(use=ipreport,atrow=13,atcol=1) "T-tilde-bar" ttildebar
	report(use=ipreport,atrow=14,atcol=1) "1% CV"	cv01
	report(use=ipreport,atrow=15,atcol=1) "5% CV"	cv05
	report(use=ipreport,atrow=16,atcol=1) "10% CV"	cv10
}
if print
   report(use=ipreport,action=show)
*
* Set the global variables
*
compute %cdstat=zbar
compute %tbar  =ttbar
*
compute %ngroup=fix(nt)
dim %%ilags(%ngroup)
compute slot=0
do i=starti,endi
   if icount(i)==0
      next
   compute slot=slot+1,%%ilags(slot)=lagsused(i)
end do i
*
end
