Basics of GAMS

Learning Outcome:

After completing this lesson, you will have basic skills in reading and editing the GAMS code used in the UNI-CGE model.

What is GAMS?

The UNI-CGE model is written in the General Algebraic Modeling System (GAMS) software. GAMS uses algebra to solve for the optimal solution to problems that involve maximizing or minimizing outcomes. Examples are maximizing producers' profits or minimizing their costs. GAMS is a powerful programming language that also provides modelers with the ability to export and display model results in ways that make it easy to communicate findings.

This course introduces you to a CGE model written in GAMS. You will not need to learn GAMS or  produce your own code.  But it is useful to develop sufficient GAMS skills to read and understand the model code, and to write simple GAMS commands when needed. 

This lesson presents basic GAMS commands and provides you with a hands-on experience in reading and writing GAMS code.


GAMS uses SETS to define the number of elements, or data values, of a variable or parameter.

Sets in the UNI-CGE model's US333 database include the set of commodities C (c-AGR, c-MFG and c-SER), the set of factors F (Land, Labor, Capital) and the set of production activities A (a-AGR, a-MFG, and a-SER).  Each of these sets has three elements.  

The names of variables and parameters include the set they are defined over. For example, the variable export quantity (QE) is defined over set C:


In the US333 database, this variable therefore has three data values. These are the export quantities of the three commodities in the model - c-AGR, c-MFG, and c-SER.  

To refer to a single element of a set, place that element's name in quotes. For example, the export quantity of the manufacturing commodity is defined as :


Variables and parameters can be defined over more than one set.  An example in the UNI-CGE model is the variable QF is expressed as: 


which is the quantity (QF) of factor F employed in production activity A. This variable has two dimensions, meaning it is defined over two sets. In the US333 database, it has nine values - the quantity of each factor employed in each of the three production activities. 

To refer to a single element of the variable, place that element's name in quotes. This example refers to the labor employed in each of the three production activities, A :


To refer to a single element in each set, place both elements' names in quotes. An example is the quantity of land employed in agriculture:


You can learn more about sets by reading the GAMS guide on set definitions.

Now you try it.

Declaring Variables and Parameters 

Variables and parameters must be introduced before they can be used in the model.  A GAMS statement "declares" their name and defines them.  Each statement ends with a semi-colon. Here are examples of GAMS code that declare the export quantity variable and an elasticity parameter:

Variable QE(c)     quantity of exports of commodity c;

Parameter ESUBVA(a)   elasticity of substitution between factors for CES production;

You can learn more about data entry by reading the GAMS guide on data entry.

Now you try it:

Assigning Values of Parameters

After parameters are declared, they are assigned values.  The values are data in the SAM or the elasticity database, or they are calculated using those data.  An example is the parameter named BUDSHR(C,H), the budget share of commodity c in the budget of household type H.  BUDSHR is calculated using data from the SAM.  It is defined as spending on commodity C by household H divided by the value of the household's total purchases.


Assigning Initial Values of Variables
Initial values are assigned to variables in a four-step process.  After the variable is declared, the modeler defines a parameter with the same name as the variable, adding a "0" (zero) suffix. Values are assigned to that parameter using data from the SAM.

Next, the initial value of the variable, with a suffix of ".L" is defined to be equal to the "0" parameter.  The .L suffix denotes that the value of the variable is both its starting point and its value after the model is solved. 

If the SAM is balanced and model calibration solves successfully, the .L value of the variable will be equal its initial "0" parameter value.  

To summarize, these are the steps in introducing a variable in the model, using QE as an example:

1. Variable is declared

          Variable QE(C)   quantity of exports of commodity c;

2. Parameter is declared

          Parameter QE0(C)  initial value of quantity of exports of commodity c;

3.  Initial value is assigned to the parameter using data from the SAM

          QE0(C)        = (SAM(C,'ROW') - SAM('EXPTAX',C));

4. Initial value is assigned to the variable

          QE.L(C) = QE0(C);

5.  Run (calibrate) the model.  Check that the  solution value QE.L(C) is equal to the parameter value = QE0(C); 

You can learn more by reading the GAMS guide on variable names and types, available HERE.

Now you try it.

Define an Experiment - Change a Productivity Parameter

An experiment redefines a parameter in the model, and the model solves for new values of all variables.  As an example, this experiment increases the level of the TFP productivity parameter (alphaa) in all production activities A by 5 percent:

alphaa(A) =alphaa(A) * 1.05;

Note the correct use of set notation and the semi-colon at the end of the statement. 

Now you try it:  

Define an Experiment - Change a Tax Rate

Tax rates in the UNI-CGE model are defined as variables, but the default closure fixes them at their initial values.  (The taxes may be made endogenous by swapping them another endogenous variable.)  Because their values are fixed, tax rates are exogenous variables that can be changed in an experiment. The model code refers to the initial value of the variable, denoted by its zero suffix.  For example, this expression redefines the tariff rate on all commodities to be 50% above their initial values:

TM0(C) = TM0(C) * 1.5;

After the model solves, the new tariff rates are named TM.L(C).

Now you try it:

Display Commands

Display commands enable you to view the values of variables and parameters.  The UNI-CGE model includes display commands for most elements that are of interest.  But you may want to insert display additional commands to check on values or to display variables or parameters that you have added to the model.  

To display a variable(s), define whether the value is the initial or the solution value by including the zero and .L suffixes. All elements in the set of that variable will be displayed.  You cannot display just one element.

For example, this command will display the initial and solution values of the quantity of imports (QM) and the quantity of exports (QE) for all commodities C:

Display qm0, qm.l, qe0, qe.l;  

To display a parameter, the command defines the parameter name.  For example, this command will display the values of the ESUBVA parameter for all activities A:

Display esubva; 

Now you try it.

Copyright Cornerstone CGE CC 4.0 BY-NC-SA
Last modified: Thursday, 2 May 2024, 9:28 PM