## calculate @mean with a list in a FOR loop

For requesting general information about EViews, sharing your own tips and tricks, and information on EViews training or guides.

Moderators: EViews Gareth, EViews Moderator

mboll
Posts: 6
Joined: Thu Feb 23, 2023 5:47 am

### calculate @mean with a list in a FOR loop

Hi!

So, here's a very basic question which I dont get to work:

I have a list of series (%j) and want to calculate a list with the mean of each of the series in the new list (%i).

%Qfp = "x1 x2 x3 x4"
PageSelect Q
For %j {%Qfp}
%i = @Mean({%j}, 2000 2022)
Next

But I not getting it to work. What am I doing wrong? How should I write it instead?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13140
Joined: Tue Sep 16, 2008 5:38 pm

### Re: calculate @mean with a list in a FOR loop

I'm not sure I follow. Do you want to create a new series where each observation of that series is the mean of the values in other series at that point in time?

If so, you need to put the series into a group, and then use the @rmean function.

MartinsFest
Posts: 22
Joined: Tue Dec 13, 2022 4:41 am

### Re: calculate @mean with a list in a FOR loop

Hello mboll,

The way Eviews work, I really wouldn't recommend storing values in lists (it is, however, well suited for storing series-names when writing program code, as you already seem to be doing.)

Regardless, here's first a couple of comments on your code, then a couple of alternatives on how to solve your problem.

- In your loop, %i will be overwritten for each iteration. It isn't assigned a unique name. Going through the example-codes below, you might better understand what I mean, and see how to store your results for each iteration.
- @Mean will return a scalar/numeric value, and you therefore cannot use "%". Instead use: "!", as in "!i" instead of "%i" in your loop.
- @Mean({%j}, 2000 2022): When wanting to look closer at a spesific time interval, I suggest you use "smpl" - see below how I usually do it. I'm actually not aware of any other way of doing this.

Alternatives on how to solve:
Here's an example on how to solve it (without using group). From your code, I assume you want an average for each series.

Code: Select all

`%Qfp = "x1 x2 x3 x4"smpl 2000 2022 For %j {%Qfp}   genr {%j}_mean = @Mean({%j})  Nextsmpl @all `

First I use "smpl" to indicate the period I am interested in when calculating mean. Notice that after the loop is finished, I return to view the whole dataset with "smpl @all".
Then, in the loop, I generate a series for each mean, thus storing the result. I use "genr {%j}_mean" to create a unique series-name, but this could as well been written as "genr mean_{%j}", or for that matter, "genr me{%j}an". Note that the value in the series will be a constant.

Here's the code for storing it in a %-string.
As a sidenote, I struggle to see the use of this (except perhaps if you're trying to make a program that eventually spits out a text-file of some sort.)

Code: Select all

`smpl 2000 2022%templist = ""For %j {%Qfp}   !i = @Mean({%j})   %templist = %templist + " " + @str(!i)Nextsmpl @allstring listedmeans = @trim(%templist)`

Here, in order to store the scalar "!i" for each iteration, I need to generate an empty string-list, "%templist", before starting the For-loop. Then I iteratively add the scalar to the list. After exiting the loop, I use "@trim(%templist)" to get rid of a blank space at the start of the string, and put it into a string-object, "string listedmeans", in case you wanted to view the result.

Following Gareth's interpretation of row-wise mean for all the series in %Qfp, here's some code covering his suggestion:

Code: Select all

`group groupname {qfp}genr meanseriesname = @rmean(groupname)`

Hope this was helpful for learning a bit more about Eviews.

mboll
Posts: 6
Joined: Thu Feb 23, 2023 5:47 am

### Re: calculate @mean with a list in a FOR loop

Thank you both so much for your answers!
This solved my problems and yes, I took one more step in knowing how eviews works. Slowly getting little better. :)

Return to “General Information and Tips and Tricks”

### Who is online

Users browsing this forum: No registered users and 1 guest