VAL

<< Click to Display Table of Contents >>

Navigation:  Gekko User Manual > Gekko statements >

VAL

Previous pageReturn to chapter overviewNext page

The VAL statement is used to assign a numeric value to a scalar variable of value type. Value names always start with the symbol %, like the other scalar types date  and string. Using the VAL keyword is no longer mandatory in Gekko 3.0.

 

Note: the VAL type is sometimes called 'val' and sometimes 'value' in this documentation. These two are the exact same thing. Note however that you must use 'val' and not 'value' as type description in function and procedure definitions, and in for loops. Note also that Gekko has no integer type: just use a VAL type.

 

Value scalars can be used in expressions, for instance in series or in PRT/MULPRT/PLOT/SHEET/CLIP statements. An integer value may be used as an annual or undated date.

 


 

Syntax

 

%v = expression;
val %v = expression;
val ?; //print val scalars

 

It is no longer legal to use for instance val v = 1.23;, omitting the %.

 


 

Example

 

You may use values as a container for fixed floating point numbers for use in your program.

 

%v1 = 1.10;
%v2 = 1/(10 + %v1);
tell '{%v1}, {%v2}';
prt %v1, %v2;
tg = %v1 * tg;
tg <2010 2013> += (%v2, -%v2, 0.5*%v2, 0.01);  //must use parentheses when not simple numbers

 

When printing values and series at the same time in a PRT statement, note that these values are held constant over any time period.

 

You can pick out individual timeseries observations with [] and put these into a value:

 

%gdp2020 = gdp[2020];

 

After this, the value %gdp2020 stores the value of series gdp in 2020.

 

You may loop over value ranges, see FOR.

 

To convert dates or strings into values, you may use the val() function.

 

You may compose the value names if you need to, using {}-curlies:

 

for val %= 1 to 3;
  %v{%i} = 100 * %i;  //defines %v1 = 100, %v2 = 200, %v3 = 300
end;
for val %= 1 to 3;
  %= %v{%i};
  tell 'Index {%i} has value {%v}';
end;

 

The result:

 

Index 1 has value 100
Index 2 has value 200
Index 3 has value 300

 

Here, the expression %v{%i} picks out the corresponding v-value. In general however, for such use column vectors (n x 1 matrices) or lists or values are recommended, cf. the identical example in the MATRIX section.

 

The following creates values from a list:

 

reset;
for string %= a, b, c;  //or: for string %i = ('a', 'b', 'c');
  %{%i} = 100;
end;
mem;

 

This creates value scalars %a, %b and %c, all with value 100.

 

 


 

Note

 

See the page with syntax diagrams if the basics of names, expressions, etc. is confusing.

 

If you need to convert a date or string scalar to a avlue type, use the val() conversion function.

 

You may use m() to indicate a missing value.

 

See also the format() function and OPTION string interpolate format val = ... ; regarding {...}-formatting of values inside strings.

 

Regarding variable types and the Gekko type system, see the VAR section. In this appendix, variable assignment rules, including variable types, is explained in more detail.

 

 


 

Related options

 

OPTION string interpolate format val = "";

 

 


 

Related statements

 

DATE, STRING, SERIES, FOR, IF