MACRO: option to require declaration of macro variables
on 05-21-201308:24 AM - last edited on 10-10-201509:59 PM by LainieH
Many computer languages require that variables are declared before use, or they offer an option to require this. This helps with writing error-free and documented code. Such a facility would be of great benefit for the SAS macro language.
SAS has two statements which create macro variables with defined scope before use. This is not exactly the same as declaration because it is not necessary to declare the use of an already-existing global variable in a macro. The existing variable creation statements %LOCAL and %GLOBAL cover only two of the three ways macro variables may be used. Therefore an additional statement is required.
I recommend the following additions:
1. A system option to force creation of macro variables before use, such as MACROCREATE. Because PROC SQL can create multiple variables with number suffixes, a way to declare such variables would be required, such as %GLOBAL indexed_*;
2. A creation statement named %INTERNAL. This creates variables which may be written ONLY locally in the current macro. The current semantics of %LOCAL allows variables to be written also by any macros called from the one where the variable is created. This new scope would cover some current uses of %LOCAL and some current uses of undeclared variables.
3. An additional facility present in other languages which would further improve the reliability of macro code would be a %STATIC scope. This would have the same scope as %INTERNAL but values would persist between macro calls. This would allow the elimination of most unstructured use of %GLOBAL variables, which can be accidentally overwritten from other parts of a program. A set of macros in a module could share information safely without the use of globals by using a macro to manage the static variables. The %STATIC statement would have to allow the setting of a default constant value.