-
Notifications
You must be signed in to change notification settings - Fork 8
/
mvartest.sas
49 lines (38 loc) · 1.56 KB
/
mvartest.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
%macro mvartest
/*----------------------------------------------------------------------
Test for the existence of a macro variable with option scope limitation
----------------------------------------------------------------------*/
(mvar /* Name of macro variable */
,scope /* Macro scope to check (optional) */
);
/*----------------------------------------------------------------------
This macro extends the functionality of the %SYMEXIST() macro function
by allowing you the check for the existance of the macro variable in
a particular macro's scope.
-----------------------------------------------------------------------
Usage:
1) Write a message when an expected macro variable is not defined.
%if ^%mvartest(abcd) %then %put macro variable ABCD is not defined.
2) Globalize the variable that holds the returned number of
observations if not defined by the user of the NOBS macro ;
%macro nobs(data,mvar=);
%if ^%mvartest(&mvar) %then %do;
%global &mvar;
%end;
... see nobs code ...
%mend nobs;
------------------------------------------------------------------------
Notes:
Returns a 1 when the macro variable exists and 0 otherwise.
----------------------------------------------------------------------*/
%local dsid rc where;
%if %length(&scope) %then %let where=scope=%upcase("&scope");
%else %let where=scope ^= "&sysmacroname" ;
%let where=name=%upcase("&mvar") and &where;
%let dsid = %sysfunc(open(sashelp.vmacro(where=(&where))));
%if (&dsid) %then %do;
%eval(%sysfunc(fetch(&dsid)) ^= -1)
%let rc = %sysfunc(close(&dsid));
%end;
%else 0;
%mend mvartest;