## Loop to run equation

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Loop to run equation

Hi, I am trying to create a same base price index for many series that starts with 100 at the most recent year and goes backwards based on the period price change. However for this to work, as it is calculated backwards I need to execute the equation many times until the first observation for each series. How can I write e loop for it that finishes at the first observation of each series ?

smpl @now-1 @now-1
aapool_reits.genr idxprice?=100
smpl @first @now-2
aapool_reits.genr idxprice?=idxprice?(1)/(1+@pch(price?(1)))

EViews Matt
EViews Developer
Posts: 495
Joined: Thu Apr 25, 2013 7:48 pm

### Re: Loop to run equation

Hello,

Mathematically, your idxprice is just a scaled version of price. There's no need to explicitly evaluate that equation backwards through your observations, just scale the data so that the target observation has a value of 100. For example,

Code: Select all

`%tmp = @datestr(@now)aapool_reits.genr idxprice? = price? * 100 / @elem(price?, %tmp)`

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Re: Loop to run equation

Great ! It worked, however I got to change the second line to aapool_reits.genr idxprice? = price? * 100 / @elem(price?, @now), as the %tmp version gives me a message "illegal date %tmp". How can I do If I want to use @now-1 or @now-4 as it does not work in this script ?
Thanks

EViews Matt
EViews Developer
Posts: 495
Joined: Thu Apr 25, 2013 7:48 pm

### Re: Loop to run equation

Curious. Perhaps the issue is that an expression such as @now-1 or @now-4 behaves differently as part of a smpl command than it does as part of a general expression. In a smpl command, the -1 or -4 are in units of your workfile's frequency, e.g., years. However, in a general expression the units match the date number returned by @now, i.e., days. In other words, in your smpl commands @now-4 means today's date minus four years, but inside @datestr @now-4 means today's data minus four days. Use @dateadd to make sure the date is changing in the units you intend. Try,

Code: Select all

`%tmp = @datestr(@dateadd(@now, -1, "YY"))aapool_reits.genr idxprice? = price? * 100 / @elem(price?, %tmp)`

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Re: Loop to run equation

Illegal date too...But that's fine, I can go with @now only. However the issue in my next steps comes when I try to deflate this series to transform it in real prices (discounted by inflation). To do this you have to go backwards, how can I do it ? The math formula is this, however Eviews can't calculate:
aapool_reits.genr idxpricereal?=idxpricereal?(1)/(1+inflation?(1))*(1+@pch(idxprice?(1)))

EViews Matt
EViews Developer
Posts: 495
Joined: Thu Apr 25, 2013 7:48 pm

### Re: Loop to run equation

Very odd.

Anyway, the regular (non-pool) genr command can calculate a series backwards, so a combination of your initial code and a loop over the crossids in the pool should do the trick. For example,

Code: Select all

`%tmp = aapool_reits.@crossidsfor %cid {%tmp}   smpl @now-1 @now-1   genr(r) idxpricereal{%cid}=100   smpl @first @now-2   genr(r) idxpricereal{%cid}=idxpricereal{%cid}(1)/(1+inflation{%cid}(1))*(1+@pch(idxprice{%cid}(1)))next`

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Re: Loop to run equation

Thanks Matt, however the results are different than the math calc. I made an excel spreadsheet with the result series calculated by Eviews and the correct one calculated by Excel, please see attached. I also adjusted your formula so it matches the previous idxprice script and the inflation (which should be divided by 100).Below the script and attached is the spreadsheet. Thanks for your help.

%tmp = aapool_reits.@crossids
for %cid {%tmp}
smpl @now @now
genr(r) idxpricereal{%cid}=100
smpl @first @now-1
genr(r) idxpricereal{%cid}=(idxpricereal{%cid}(1))/((1+@pch(idxprice{%cid}(1)))*(1+inflation{%cid}(1)/100))
next
Attachments
Eviews.xlsx

EViews Matt
EViews Developer
Posts: 495
Joined: Thu Apr 25, 2013 7:48 pm

### Re: Loop to run equation

I don't know what calculations you performed in Excel, so I can't say why you're getting different results in EViews. Chances are that either the Excel formula or the EViews formula is not calculating what you think it is.

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Re: Loop to run equation

Matt,
The equation that I want to calculate and which is shown in the Excel spreadsheet I attached in the last post is: idxpricereal = idxpricereal(1)/((1+(idxprice(1)/idxprice-1))*(1+inflation(1)/100)). However the script below does not have show the same results of this former equation.

genr(r) idxpricereal{%cid}=(idxpricereal{%cid}(1))/((1+@pch(idxprice{%cid}(1)))*(1+inflation{%cid}(1)/100))

EViews Matt
EViews Developer
Posts: 495
Joined: Thu Apr 25, 2013 7:48 pm

### Re: Loop to run equation

The formula in your Excel file is missing the parentheses around the denominator.

fmantessof
Posts: 24
Joined: Fri Aug 14, 2015 7:54 am

### Re: Loop to run equation

Issue solved !..Thanks Matt, sorry for the parenthesis error