Scalars

<< Click to Display Table of Contents >>

Navigation:  Gekko User Guide > Data Management User Guide > Gekko programs >

Scalars

Previous pageReturn to chapter overviewNext page

As explained in the section on frequency and time period, scalar variables can practical for storing general settings in the beginning of a system of Gekko program files. Scalars are useful in longer program files, where you may otherwise lose track of for instance time settings, file paths etc. Scalars can be of type value, date or string, and scalars always start with the symbol %.

 

Note: Gekko variables can also be of collection type, for instance list type. Lists can also be practical to define at the beginning of a system of Gekko program files (lists are explained in the following user guide section on lists).

 

Gekko has three types of scalars:

 

value

Numeric values, with decimals (also used for integer values).
For instance %= 25, %= -1.2345, or %= 2.57e9.

date

Date variable with time indications.
For instance %= 2012a, %= 2012q4 or %= 2012m12.
If you write %= 2012, %d will technically be value, but can still be used just like an annual date.

See also "date combining functions" under functions.

string

For example text strings or path names.

For instance %= 'Price index, 100 = 1' or %= 'm:\data\revision2\'.

See also "string combining functions" under functions.

 

Notes:

 

Values: a particular observation from a timeseries can easily be fetched as a value: %= x[2020];, where x is a timeseries. In Gekko, there is no special integer type: for integers, you just use values. Values often represent annual dates, too.

Dates: you may add or deduct integers from dates, for instance the quarterly %d1 = 2012q4; %d2 = %d1 + 10; will add 10 periods/quarters to %d1, with result %d2 = 2015q2 (fourth quarter of 2012 + 10 periods = second quarter of 2015). You may also use date differences: %d2 - %d1 = 10.

Strings: can be concatenated with +, for instance %= 'rise'; prt 'sun' + %s;, which prints 'sunrise'. Alternatively, and often more readable, Gekko also supports string interpolation, where Gekko replaces any {...} curlies inside a string with the result of evaluating the curlies. For instance, %= 'rise'; prt 'sun{%s}'; will also print 'sunrise'. String interpolation corresponds tightly with the equivalent concept of name-composition in Gekko, where %= 'rise'; prt sun{%s}; will try to find a timeseries with name sunrise  (note the absense of quotes in this PRT variant).

Types: you may prepend explicit scalar type, for instance val %= 100;, but normally this is not necessary, because the scalar type is normally given from the right-hand side of the assignment. Beware that, in general, if you add a value and a string, the value will first be promoted to a string, and therefore prt '2' + 3;, which print '23', not 5. Therefore, you may sometimes need to convert, which is done with the functions val(), date() or string(). These functions try to convert to the desired type, for instance prt val('2') + 3; will print 5.

 

Example:

 

%bank = 'hist1115';
read %bank;
fy <2010 2012> += 1000;
write %bank;

 

The series fy from the bank hist1115.gbk is augmented by 1000 in 2010-12. Here, we do not need to use {}-curlies around %bank, (but still this is perfectly legal), because the READ and WRITE statements (and IMPORT and EXPORT, too) support both of the two variants read hist1115; or read 'hist1115';.

 

In connection with data revision programs, it may be a good idea to incorporate the names of input banks, output banks and time period settings centrally at the top of a program file, so that it is easy to adjust.

 

// --- Update banks and dates if necessary --- 
cls; reset;
%in = 'in.gbk'; // input bank
%out = 'out.gbk'; // output bank
%per1 = 2012;
%per2 = 2014;
 
// ---- Does not normally need to change ---
read %in;
time %per1 %per2;
var1 %= 2;  //2 percent growth rate
var3 = var1 + var2;
write <%per1 %per2> var3 file=%out;
reset;

 

In the above example, an input databank %in is read, time is set to the chosen period, the growth rate of the series var1 is set to 2% annually, and var3 is calculated as the sum of var1 and var2. Finally, a databank is written, with the given %out name.

 

If this needs to be performed on two other databanks, or on a different time period, this can easily be adjusted in the top part of the program.