# DRY: Some useful macros in Gams

DRY stands for “Don’t repeat yourself” and is one of the main principles of efficient programming. In Gams, I use some checks over and over again. Instead of rewriting the code or searching for a file with the existing code and copying it, I use macros in Gams. Macros aren’t difficult to write. You can find more on them here in the documentation.

Here is a simple example from the Gams documentation defining and using a macro that calculates the reciprocal of a number:

More complex macros can go over several lines as long as every line, with the exception of the last line, ends with a backslash.
Here is one of my favorites: Whenever I want to check if allrow sums are equal to the the corresponding column sums (a typical check if you work with Input-Output tables), I use the following macro:

As you can see, every code line ends with a backslash. You can add comments, but be sure not to use empty lines. In my code, I can now use this matrix as follows:

where s is the row index and is aliased with as. If there is any inconsistency between the sums, the code aborts and Gams reports the number of inconsistencies and the differences themselves.

Another macro I use often is the checkmapping macro. I described how this macro functions in a previous post. It checks if I did not make a mistake in my mapping from one set to another. The macro looks like this:

The macro aborts the code if I made a mistake and shows the set elements that cause problems.
Another example is checking if the absolute value of some parameter is zero (for example, if I am checking if there are differences in a zero-profit condition) and if not aborts the code:

Another example is checking if the absolute value of some parameter is zero (for example, if I am checking if there are differences in a zero-profit condition) and if not aborts the code:

In Gams this could be used like this:

which would produce an abort if there would be a difference between lhs and rhs:

—- 481 Difference greater than 1E-7
PARAMETER diff = 0.010 Difference
**** Exec Error at line 481: Execution halted: abort\$1 ‘Difference greater than 1E-7’

The macros are stored in a file “macros.gms” that I include in my code like this