Subroutine time-shifting a sample object

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

mamo
Posts: 77
Joined: Wed Nov 07, 2012 9:11 am

Subroutine time-shifting a sample object

Postby mamo » 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

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

mamo
Posts: 77
Joined: Wed Nov 07, 2012 9:11 am

Re: Subroutine time-shifting a sample object

Postby mamo » Thu Jul 06, 2017 8:30 am

Updated: includes error control, and now also usable with undated pages
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)
' Error handling: Program stops with an error message if there is an inconsistent pair of period definitions
'               (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}) s1.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
   s1.set {%ddef} {%if}
   'adjust workfile sample to the revised sample if so requested
   if !setsmpl then
      smpl s1
   endif
endsub

mamo
Posts: 77
Joined: Wed Nov 07, 2012 9:11 am

Re: Subroutine time-shifting a sample object

Postby mamo » Fri Jul 07, 2017 1:20 am

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: Select all

'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





Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 2 guests