Using strings to name variables

For questions regarding programming in the EViews programming language.

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

bobshackleton
Posts: 25
Joined: Wed Mar 23, 2011 6:50 am

Using strings to name variables

Postby bobshackleton » Wed Mar 30, 2011 3:57 pm

I'm writing a global subroutine to which I have sent, among other things, a GROUP that the subroutine brings in as 'data'. I would like to use the names of the series in the group as strings, and concatenate those strings with other strings to call other series that are not in the group (but have similar names) and do calculations on them. Imagine there are only 2 series in 'data' and they're called SERIESA and SERIESB. My intention with this loop is to calculate

Code: Select all

( @pca(QSERIESA) * SERIESA(-1) / AGGREGATE(-1) ) + ( @pca(QSERIESB) * SERIESB(-1) / AGGREGATE(-1) )
so that I am using the name SERIESA to call QSERIESA as well. I wrote the code this way but it doesn't work; the error message states that Eviews "can not assign string expression to numeric variable...." :

Code: Select all

for !x = 1 TO data.@count SERIES laspq = laspq + (@pca("q"+data.@seriesname(!x)) * data.@seriesname(!x)(-1) / aggregate(-1)) next
I'm probably making several different mistakes all at once but could you please explain what I need to do to differently? Thanks very much for your help.

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

Re: Using strings to name variables

Postby EViews Gareth » Wed Mar 30, 2011 4:09 pm

Code: Select all

data.@seriesname(!i)
returns a string containing the name of the series. It does not return the series itself. When you write an expression involving a series, you write the actual series, not a string containing the series name. I know that sounds subtle, but essentially what you are doing is:

Code: Select all

@pca("q"+"SERIESA") * "SERIESA"(-1)
where you want to be doing:

Code: Select all

@pca(QSERIESA) * SERIESA(-1)
Note the quotes in the first one around the series name (since it is a string).

There are a couple of ways to fix this. The most general is with the following:

Code: Select all

for !x = 1 TO data.@count %name1 = data.@seriesname(!x) %name2 = "Q" + %name1 SERIES laspq = laspq + (@pca({%name2}) * {%name1}(-1) / aggregate(-1)) next
where I am storing the name of the series in a program variable, %name, and then using { } to de-reference that name (i.e., in very simple terms, remove the quotes).


Note if you had another group with the Q variables in it, you can probably just avoid using the name of the series at all:

Code: Select all

for !x = 1 TO data.@count SERIES laspq = laspq + (@pca(qdata(!x)) * data(!x)(-1) / aggregate(-1)) next
Where you can use group(!i) to refer to the ith series in a group.

bobshackleton
Posts: 25
Joined: Wed Mar 23, 2011 6:50 am

Re: Using strings to name variables

Postby bobshackleton » Thu Mar 31, 2011 7:37 am

This is extremely helpful - thank you very much!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests