Referring to a scalar from a different page

For questions regarding programming in the EViews programming language.

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

Fifis
Posts: 23
Joined: Thu Jan 06, 2022 11:48 pm

Referring to a scalar from a different page

Postby Fifis » Wed Apr 20, 2022 8:44 am

Dear all,

I am trying to increase readability (and debuggability) of some inherited code, and am currently having trouble using scalars from other pages of a work file.

The goal is to avoid using %vars and !vars because they are local, and vanish after the execution has been finished. There are chunks of code that need to be run independently (after some heavy estimation has been carried out), and my goal is to not lose some values that are used throughout the document.

The following does not work:

Code: Select all

wfcreate(wf = "exports_factor_model_avk", page = numbers) u 1
scalar year_min = 1990
scalar year_max = 2025
scalar quarter_max = 4
pagecreate(page = dat_qrt) q {year_min}:1 {year_max}:{quarter_max}
pageselect dat_qrt
<some stuff goes on>
pagecreate(page = dat_mth) m {numbers\year_min}:1 {numbers\year_max}:{numbers\month_max}


Error: NUMBERS\YEAR_MIN is not a valid string or scalar name

Am I missing some brackets or extra symbols? Somehow `pagecreate` could read the values from the same page, but not from a different one.

In subsequent calculations, I need to re-use the scalars saved in the `numbers` page: there are many loops and other documents, and I need to do calculations on one page whilst invoking the scalars from the `numbers` page without switching. There must be an easy solution where one would not need to select a page with scalars in order to use those values, and I could not find a similar example in the manual.

Many thanks in advance!

Yours sincerely,
Andreï V. Kostyrka

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

Re: Referring to a scalar from a different page

Postby EViews Gareth » Wed Apr 20, 2022 1:02 pm

You would need to copy the scalar objects from one page to the page you are currently working with, using the copy command.

This is precisely why !variables exist.
Follow us on Twitter @IHSEViews

Fifis
Posts: 23
Joined: Thu Jan 06, 2022 11:48 pm

Re: Referring to a scalar from a different page

Postby Fifis » Thu Apr 21, 2022 1:11 am

Dear Gareth, thank you for your quick reply!

So is it not possible to use scalars from one sheet for calculations in a different sheet? This seems to be quite common even in Excel, e.g. to reference a fixed cell in a different helper sheet to carry out the computations with a set of fixed values.

Maybe this question can be rephrased: is there a way to conveniently define a set of scalars (constants and strings) at the beginning so that they always stay somewhere in the work file, and to use them in intermediate calculations in different pages, and then, save new scalars without resorting to temporary !variables for every scalar that is to be used?

Here is my attempt (goal: compute the mean and SD because they are reused later in the code):

Code: Select all

for !i=1 to inuse.@count
   %sn = inuse.@seriesname(!i)
   !m = @mean({%sn})
   !s = @stdev({%sn})
   pageselect numbers
   scalar mean_{%sn} = !m
   scalar sdev_{%sn} = !s
   pageselect dat_mth
   series {%sn}_s = ({%sn} - !m) / !s
next


I am asking because in R, one would simply write a one-liner like `numbers <- lapply(dat_mth[, inuse], function(x) c(m = mean(x), s = sd(x)))` and then, use `numbers[["gdp"]]["m"]` in any place of the code because this object would be saved globally to read the values, and `numbers$stuff <- somethingnew(somedata)` to add new values.

What is the best way to translate this idea into EViews without too much hardcoding and redundancies that I fail to avoid?

Fifis
Posts: 23
Joined: Thu Jan 06, 2022 11:48 pm

Re: Referring to a scalar from a different page

Postby Fifis » Thu Apr 21, 2022 2:12 am

The problem is, the number of lines in the code grows with the number of !variables that have to be loaded, and I would appreciate any hint about how to transform

Code: Select all

pageselect numbers
!ymin = year_min_estim
!ymax = year_max_estim
!mmin = month_min_estim
!mmax = month_max_estim
pageselect dat_mth
smpl !ymin:!mmin !ymax:!mmax


into something as simple as

Code: Select all

pageselect dat_mth
smpl {numbers\year_min_estim}:{numbers\month_min_estim} {numbers\year_max_estim}:{numbers\month_max_estim}

(which unfortunately does not work)?

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

Re: Referring to a scalar from a different page

Postby EViews Gareth » Thu Apr 21, 2022 7:55 am

Ignoring the fact that the smpl statement is illegal either way, if you want to use scalar objects, you'll have to copy them:

Code: Select all

copy numbers\*estim dat_mth\*estim
Follow us on Twitter @IHSEViews

Fifis
Posts: 23
Joined: Thu Jan 06, 2022 11:48 pm

Re: Referring to a scalar from a different page

Postby Fifis » Fri Apr 22, 2022 8:32 am

Many thanks, it works!


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 12 guests