Subroutine time-shifting a sample object
Posted: Thu Jul 06, 2017 5:04 am
Dear Eviews users,
the program below contains the subroutine "shiftsmpl" to shift the lower and upper boundaries in pairs of period definitions in a sample object easily.
This could be useful within a program applying statistical/ econometric analyses over rolling samples.
The current version of the subroutine works only with dated or panel pages and does not yet check if the condition "lower boundary < upper boundary" still holds for each of the period pairs in the shifted sample.
Best,
mamo
the program below contains the subroutine "shiftsmpl" to shift the lower and upper boundaries in pairs of period definitions in a sample object easily.
This could be useful within a program applying statistical/ econometric analyses over rolling samples.
The current version of the subroutine works only with dated or panel pages and does not yet check if the condition "lower boundary < upper boundary" still holds for each of the period pairs in the shifted sample.
Best,
mamo
Code: Select all
'Example of using shiftsmpl
'Create wf
wfcreate q 2000Q1 2016Q4
'Create an indicator variable
series indicator=nrnd>0
'Defining sample s1
sample s1 2002Q1 2010Q4 if indicator=1
'Set the current page sample to s1
smpl s1
'change the sample s1 by shifting its lower bound by 2 quarters forward and the upper bond by 1 quarter backward
'and set the current page sample to the revised sample s1, ie to "2002Q3 2010Q3 if indicator=1"
call shiftsmpl(s1,2,-1,1)
subroutine shiftsmpl(sample s, scalar !n1, scalar !n2, scalar !setsmpl)
'**********************************************************************************************************
' Does: shifts the time frame of the sample object s, and optionally changes the current sample
' to the revised sample s
' s: sample, sample object
' !n1: integer, shift of lower time boundary
' !n2: integer, shift of upper time boundary
' !setsmpl: integer, switch to indicating if the current sample should be changed to
' the shifted sample s (>0) or not (<=0)
' Notes: "if"-clauses in the definition of s are preserved
' The shifting is in terms of the freqency of the current workpage
' The shift parameters !n1 and !n2 are applied pairwise to each pair of period definitions in s
' !n1 and !n2 can obtain positive (forward shift) or negative values (backward shift)
' Created: 06/2017
'**********************************************************************************************************
%stxt="_$stext"
freeze(mode=overwrite, {%stxt}) s1.spec
%sstr=@stripquotes(@trim(@wjoin({%stxt}.@svector)))
delete {%stxt}
if @instr(@trim(%sstr),"if") then
%if=@trim(@right(%sstr, @len(%sstr)+1-@instr(@trim(%sstr),"if")))
%sstr=@trim(@left(%sstr, @instr(@trim(%sstr),"if")-1))
else
%if=""
endif
!n=@wcount(%sstr)
%s= @left(@trim(@word(%sstr,!n)),2)
%ddef=""
for !i=1 to !n
!j=2-@floor(@mod(!i,2))
%d=@word(%sstr,1)
%sstr=@trim(@wdrop(%sstr, %d))
%d=@datestr(@dateadd(@dateval(%d),!n{!j}, @pagefreq))
%ddef=%ddef+%d+" "
next
%ddef=@trim(%ddef)
s1.set {%ddef} {%if}
if !setsmpl then
smpl s1
endif
endsub