Page 1 of 1

how to run function for variables starting with the same letter

Posted: Wed Oct 19, 2016 10:34 am
by Gin
Hi all,

I am a new Eviews user. I have 14 variables names with the same pattern. They all start with "d_", e.g. d_capacity, d_trade, d_gdp, etc. I need to do "scalar sum = @sum(d_capacity)
scalar sum = @sum(d_trade)
scalar sum = @sum(d_gdp)
..."
for all these 14 variables. Is there a way to carry out this calculation in a loop?

A hint would be helpful. Thanks

Re: how to run function for variables starting with the same letter

Posted: Wed Oct 19, 2016 10:54 am
by EViews Gareth
File->New Program, then write the following and hit Run

Code: Select all

for %j capacity trade gdp
scalar sum_{%j} = @sum(d_{%j})
next

Re: how to run function for variables starting with the same letter

Posted: Wed Oct 19, 2016 1:04 pm
by Gin
Thanks Gareth!

Is there in Eviews a way expressing "for `var' in varlist" as in stata?

I am trying to fill these scalars into a vector or a matrix.

But it seems that the following code doesn't work. I think it is because Eviews needs the scalar to have specific numbering.

vector(3) n_obs

for %j capacity trade gdp
n_obs.fill = sum_{%j}
next

Re: how to run function for variables starting with the same letter

Posted: Wed Oct 19, 2016 1:43 pm
by EViews Glenn
I may be missing something, but it seems to me as though you want something like

Code: Select all

vector(3) n_obs
!i = 1
for %j d_capacity d_trade d_gdp
   n_obs(!i) = @sum({%j})
   !i = !i + 1
next

There's another way of doing this by loading the elements into a svector. I prefer it in this case since you don't have to increment the !i in the loop yourself.

Code: Select all

svector list = @wsplit("d_capacity d_trade d_gdp")
vector(@rows(list)) n_obs
for !i = 1 to @rows(list)
   %name = list(!i)
   n_obs(!i) = @sum({%name})
next

Re: how to run function for variables starting with the same letter

Posted: Wed Oct 19, 2016 1:57 pm
by EViews Gareth
I prefer:

Code: Select all

%list = "d_capacity d_trade d_gdp"
for !i = 1 to @wcount(%list)
   %name = @word(%list, !i)
   n_obs(!i) = @sum({%name})
next

Re: how to run function for variables starting with the same letter

Posted: Thu Oct 20, 2016 2:33 am
by Gin
Thanks Glenn and Gareth! :)