how to run function for variables starting with the same letter

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

Moderators: EViews Gareth, EViews Moderator

Gin
Posts: 22
Joined: Wed Oct 19, 2016 10:26 am

how to run function for variables starting with the same letter

Postby Gin » Wed Oct 19, 2016 10:34 am

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

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

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

Postby EViews Gareth » Wed Oct 19, 2016 10:54 am

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
Follow us on Twitter @IHSEViews

Gin
Posts: 22
Joined: Wed Oct 19, 2016 10:26 am

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

Postby Gin » Wed Oct 19, 2016 1:04 pm

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

EViews Glenn
EViews Developer
Posts: 2530
Joined: Wed Oct 15, 2008 9:17 am

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

Postby EViews Glenn » Wed Oct 19, 2016 1:43 pm

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

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

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

Postby EViews Gareth » Wed Oct 19, 2016 1:57 pm

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
Follow us on Twitter @IHSEViews

Gin
Posts: 22
Joined: Wed Oct 19, 2016 10:26 am

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

Postby Gin » Thu Oct 20, 2016 2:33 am

Thanks Glenn and Gareth! :)


Return to “General Information and Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests