If you can’t do without my blog posts and don’t want to check if there are any new posts, you can subscribe to this blog. Just scroll down in the widget bar on the right side until you find the following:
Add your email address and wait for the confirmation email.
Gams makes aggregating tables quite easy. Imagine you a table with data for 100 sectors but you want to run your model in the testing phase for an aggregation of these sectors (e.g. an aggregate the 100 sectors to the three sector groups “agriculture”, “industry”, and “services”). This is a typical situation in CGE (computable general equilibrium modeling): you have a social accounting matrix for your country and you want to start with a simple model having only a few sectors, one household, no taxes, and no government). In Gams you simply introduce a mapping that maps the 100 sectors to the three groups. Once you have this mapping, producing the aggregated table of the data is just a one-liner.
What follows is the code to import a table from Excel, aggregate the table and export it.The Excel table is called sam.xlsx and contains the following table in sheet1:
The following file “data.gms” produces an aggregated table:
alle all elements of the SAM table
set agg'aggregated set with just one sector'/sec,RA,capital,labor/,
mapAlle_Agg(alle,agg)'Mapping the disaggregated to the aggregated'
You can make this procedure flexible by defining a compile-time variable, so you can have multiple aggregations (you probably want to have your data for the full table and an aggregated table). For this, we enter the following code at the beginning of the file:
$setlocal mapping agg1
This defines the local compile-time variable “mapping” which has the value “agg1”. When you compile/execute your file, Gams will replace %mapping% in your code with the actual value of the variable “mapping”.
We move the mapping (lines 6-11) in a separate file called “agg1.inc” and in the main file we put here instead the following line:
Gams will now replace %mapping% by “agg1” and include the file “agg1.inc”. We also change the line, where we export the results as follows, so Gams will export the results to the file “agg1.gdx”:
Additionally, we add a mapping file with the full disaggregation (full.inc), which maps each element to itself:
set agg'Aggregated elements'/sec1,sec2,RA,capital,labor/,
We now can produce a gdx with the full table by replacing “agg1” by “full” (or run the file in the command line as “gams data.gms –mapping=full”):
$setlocal mapping full
We now will have a gdx file “full.gdx” with the full table.
Now, we can run our model code in the full disaggregated or aggregated version. For this, you will have to put the following lines at the start of your model file: