Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_assertscope.sas File Reference

Used to capture scope leakage of macro variables. More...

Go to the source code of this file.

Detailed Description

A common 'difficult to detect' bug in macros is where a nested macro over-writes variables in a higher level macro.

This assertion takes a snapshot of the macro variables before and after a macro invocation. Differences are captured in the &outds table. This makes it easy to detect whether any macro variables were modified or changed.

The following variables are NOT tested (as they are known, global variables used in SASjs):

  • &sasjs_prefix._FUNCTIONS

Global variables are initialised in mp_init.sas - which will also trigger "strict mode" in your SAS session. Whilst this is a default in SASjs produced apps, if you prefer not to use this mode, simply instantiate the following variable to prevent the macro from running: SASJS_PREFIX

Example usage:

%mp_assertscope(SNAPSHOT)

%let oops=I did it again;

%mp_assertscope(COMPARE,
  desc=Checking macro variables against previous snapshot
)

This macro is designed to work alongside sasjs test - for more information about this facility, visit cli.sasjs.io/test.

Parameters
[in]action(SNAPSHOT) The action to take. Valid values:
  • SNAPSHOT - take a copy of the current macro variables
  • COMPARE - compare the current macro variables against previous values
[in]scope=(GLOBAL) The scope of the variables to be checked. This corresponds to the values in the SCOPE column in sashelp.vmacro.
[in]desc=(Testing scope leakage) The user provided test description
[in]ignorelist=Provide a list of macro variable names to ignore from the comparison
[in,out]scopeds=(work.mp_assertscope) The dataset to contain the scope snapshot
[out]outds=(work.test_results) The output dataset to contain the results. If it does not exist, it will be created, with the following format:
TEST_DESCRIPTION:$256 TEST_RESULT:$4 TEST_COMMENTS:$256
User Provided description PASS No out of scope variables created or modified

SAS Macros

Related Macros

Version
9.2
Author
Allan Bowe

Definition in file mp_assertscope.sas.