Page 1 of 1

loop and time series control

Posted: Thu Jan 31, 2013 11:29 am
by coder_79
A very simple, intuitive conversion from returns to index levels
Quarterly data runs from 1987Q1 to 2011Q4
program shown as below

**************************************************************************************************************************
series level

for !i=1 to 100
if !i=1 then
level.fill(o=1987Q1) @elem(return, "1987Q1")/100+1 ' returns are in percentage, base level of 1986Q4 equals 1
else
level.fill(o=1986Q4+!i) (@elem(level, 1986Q4+!i-1)*(1+@elem(return, 1986Q4+!i)/100))
endif
next
***************************************************************************************************************************

however, eviews reports error for 1986Q4+!i, saying invalid date.
tried several date functions, including @otod, cannot figure it out.
Any one can help? Thanks.

Re: loop and time series control

Posted: Thu Jan 31, 2013 11:41 am
by coder_79
I'm using eviews 7.2

Re: loop and time series control

Posted: Thu Jan 31, 2013 11:53 am
by EViews Gareth
The precise fix to your code is:

Code: Select all

for !i=1 to 100 if !i=1 then level.fill(o=1987Q1) @elem(return, "1987Q1")/100+1 ' returns are in percentage, base level of 1986Q4 equals 1 else %date1 = @datestr(@dateadd(@dateval("1986q4", "YYYY[q]Q"),!i-1,"q")) %date2 = @datestr(@dateadd(@dateval("1986q4", "YYYY[q]Q"),!i,"q")) level.fill(o=%date2) (@elem(level, %date1)*(1+@elem(return, %date2)/100)) endif next
However that is a silly way to go about things. You're better off doing this:

Code: Select all

smpl 1987q1 1987q1 series level2 = return/100+1 smpl 1987q2 @last level2 = level(-1)*(1+return/100) smpl @all

Re: loop and time series control

Posted: Thu Jan 31, 2013 12:07 pm
by coder_79
Thank you very much!

Re: loop and time series control

Posted: Thu Jan 31, 2013 12:20 pm
by EViews Gareth
If you want to get really fancy, you can do it in just one line:

Code: Select all

series level3 = @recode(level3(-1)=na,return/100+1, level(-1)*(return/100+1))