Final correction
Error corrected: the sample object "s1" in the code of the subroutine "shiftsmpl" was corrected to "s", in line with the sample object "s" provided as an argument to the subroutine.
Best,
mamo
Code:
'Example of using shiftsmpl
'Create wf
wfcreate q 2000Q1 2016Q4
'Create an indicator variable
series indicator=nrnd>0
'Defining sample s1
sample sampl1 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, indicating if the current sample should be changed to
' the shifted sample s (!setsmpl>0) or not (!setsmpl<=0)
' Error handling: Program stops with an error message when an inconsistent pair of period definitions is encountered
' (i.e., lower boundary > upper boundary
' Sideeffects: a text object named "_$stext" is created and deleted.
' 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"
' Read sample definition into string %sstr
freeze(mode=overwrite, {%stxt}) s.spec
%sstr=@stripquotes(@trim(@wjoin({%stxt}.@svector)))
'delete the temporary text object
delete {%stxt}
' Seperate out the "if"-clause into string %if (if there is any)
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
' count the number of period boundaries (must be even number)
!n=@wcount(%sstr)
' apply pairwise period shifts n1 and n2
%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))
'check if the current period pair confirms with lower boundary <= upper boundary
if !j=2 then
%dprev=@word(%ddef, !i-1)
if @datediff(@dateval(%d), @dateval(%dprev), @pagefreq) < 0 then
'stop if the recent pair of periods is not confirming with lower boundary <= upper boundary
@uiprompt("Error in shiftsmpl - inconsistent sample definition: "+%d+"<"+%dprev, "O")
return
endif
endif
%ddef=%ddef+%d+" "
next
%ddef=@trim(%ddef)
're-set the sample to the shifted period definitions
s.set {%ddef} {%if}
'adjust workfile sample to the revised sample if so requested
if !setsmpl then
smpl s
endif
endsub