Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_assertscope.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Used to capture scope leakage of macro variables
4 @details
5
6 A common 'difficult to detect' bug in macros is where a nested macro
7 over-writes variables in a higher level macro.
8
9 This assertion takes a snapshot of the macro variables before and after
10 a macro invocation. Differences are captured in the `&outds` table. This
11 makes it easy to detect whether any macro variables were modified or
12 changed.
13
14 The following variables are NOT tested (as they are known, global variables
15 used in SASjs):
16
17 @li _SASJS_FUNCTIONS
18
19 Global variables are initialised in mp_init.sas - which will also trigger
20 "strict mode" in your SAS session. Whilst this is a default in SASjs
21 produced apps, if you prefer not to use this mode, simply instantiate the
22 following variable to prevent the macro from running: `_SASJS_INIT`
23
24 Example usage:
25
26 %mp_assertscope(SNAPSHOT)
27
28 %let oops=I did it again;
29
30 %mp_assertscope(COMPARE,
31 desc=Checking macro variables against previous snapshot
32 )
33
34 This macro is designed to work alongside `sasjs test` - for more information
35 about this facility, visit [cli.sasjs.io/test](https://cli.sasjs.io/test).
36
37 @param [in] action (SNAPSHOT) The action to take. Valid values:
38 @li SNAPSHOT - take a copy of the current macro variables
39 @li COMPARE - compare the current macro variables against previous values
40 @param [in] scope= (GLOBAL) The scope of the variables to be checked. This
41 corresponds to the values in the SCOPE column in `sashelp.vmacro`.
42 @param [in] desc= (Testing scope leakage) The user provided test description
43 @param [in] ignorelist= Provide a list of macro variable names to ignore from
44 the comparison
45 @param [in,out] scopeds= (work.mp_assertscope) The dataset to contain the
46 scope snapshot
47 @param [out] outds= (work.test_results) The output dataset to contain the
48 results. If it does not exist, it will be created, with the following format:
49 |TEST_DESCRIPTION:$256|TEST_RESULT:$5|TEST_COMMENTS:$256|
50 |---|---|---|
51 |User Provided description|PASS|No out of scope variables created or modified|
52
53 <h4> SAS Macros </h4>
54 @li mddl_assert.sas
55 @li mf_getuniquename.sas
56 @li mf_getquotedstr.sas
57 @li mp_init.sas
58
59 <h4> Related Macros </h4>
60 @li mp_assert.sas
61 @li mp_assertcols.sas
62 @li mp_assertcolvals.sas
63 @li mp_assertdsobs.sas
64 @li mp_assertscope.test.sas
65
66 @version 9.2
67 @author Allan Bowe
68
69**/
70
71%macro mp_assertscope(action,
72 desc=Testing Scope Leakage,
73 scope=GLOBAL,
74 scopeds=work.mp_assertscope,
75 ignorelist=,
76 outds=work.test_results
77)/*/STORE SOURCE*/;
78/**
79 * this sets up the global vars, it will also enter STRICT mode. If this
80 * behaviour is not desired, simply initiate the following global macro
81 * variable to prevent the macro from running: _SASJS_INIT
82 */
83%mp_init()
84
85%local ds test_result test_comments del add mod ilist;
86%let ilist=%upcase(_SASJS_FUNCTIONS SYS_PROCHTTP_STATUS_CODE
87 SYS_PROCHTTP_STATUS_CODE SYS_PROCHTTP_STATUS_PHRASE &ignorelist);
88
89/* get current variables */
90%if &action=SNAPSHOT %then %do;
91 proc sql;
92 create table &scopeds as
93 select name,offset,value
94 from dictionary.macros
95 where scope="&scope" and upcase(name) not in (%mf_getquotedstr(&ilist))
96 order by name,offset;
97 quit;
98%end;
99%else %if &action=COMPARE %then %do;
100
101 proc sql;
102 create table _data_ as
103 select name,offset,value
104 from dictionary.macros
105 where scope="&scope" and upcase(name) not in (%mf_getquotedstr(&ilist))
106 order by name,offset;
107
108 %let ds=&syslast;
109
110 proc compare
111 base=&scopeds(where=(upcase(name) not in (%mf_getquotedstr(&ilist))))
112 compare=&ds noprint;
113 run;
114
115 %if &sysinfo=0 %then %do;
116 %let test_result=PASS;
117 %let test_comments=&scope Variables Unmodified;
118 %end;
119 %else %do;
120 proc sql noprint undo_policy=none;
121 select distinct name into: del separated by ' ' from &scopeds
122 where name not in (select name from &ds);
123 select distinct name into: add separated by ' ' from &ds
124 where name not in (select name from &scopeds);
125 select distinct a.name into: mod separated by ' '
126 from &scopeds a
127 inner join &ds b
128 on a.name=b.name
129 and a.offset=b.offset
130 where a.value ne b.value;
131 %let test_result=FAIL;
132 %let test_comments=%str(Mod:(&mod) Add:(&add) Del:(&del));
133 %end;
134
135 %let ds=work.%mf_getuniquename(prefix=mp_assertscope);
136 %mddl_assert(&ds)
137 data &ds;
138 if 0 then set &ds;
139 test_description=symget('desc');
140 test_comments=symget('test_comments');
141 test_result=symget('test_result');
142 run;
143
144 proc append base=&outds data=&ds;
145 run;
146 proc sql;
147 drop table &ds;
148 quit;
149%end;
150
151%mend mp_assertscope;