Macros are a way to repeat model structures without retyping equations. To do this, the macro is first defined by writing equations, and then invoked by calling them as you would call a function. In many cases, they can simplify the modeling process, but they are also dangerous in that they can allow dynamics to "creep into" a model. The implementation of macros in Vensim overcomes much of this hidden dynamics problem. Still, it is recommended that macros be used sparingly, if at all.
NOTE The macro definition must occur before the macro is referenced.
This is the only equation ordering rule in the Vensim modeling language. If you do not define a macro before you use it, you will receive a syntax error message.
Macro definitions can be nested, that is the definition of a macro can involve another macro. In this case when Vensim expands the macro to create the detailed equations it does the expansion recursively.
Since the expansion of a Macro results in additional model variables, it is necessary to create names for those variables. Vensim follows a convention of using the left hand side variable as the prefix to the macro, followed by the macro name, and then the internal macro variables. Internal macro variables are visible only if you have selected this option in the Setting tab of the global options. In this case the equation:
Smoothed Sales = SMOOTH(sales, time to smooth sales)
would show as its causes
|time to smooth sales|
If the option to expand macro variables were not selected the second entry would be skipped altogether. Note that if two Macro variables would have the same name a 3 digit number precedes the Macro name in the creation of the new variable name in order to assure uniqueness.
There is no support for Subscripts within Macro definitions. When a Macro is called, the variables should be used with the same subscripts they appear with normally. When the macro equation is expanded, the created variables inherit the Subscripts from the left hand side of the equation. Each equation created during the expansion of the Macro has the same subscripts on the left hand side.
A number of built in function in Vensim are defined as Macros. These include: DELAY1, DELAY1I, DELAY3, DELAY3I, DELAYP, FORECAST, NPV, NPVE, SMOOTH, SMOOTH3, SMOOTH3I and TREND.