-
Notifications
You must be signed in to change notification settings - Fork 8
/
symget.sas
90 lines (83 loc) · 3.85 KB
/
symget.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
%macro symget
/*----------------------------------------------------------------------
Get value for macro variable that is hidden by local macro variable
----------------------------------------------------------------------*/
(mvar /* Macro variable name */
,include= /* Space delimited list of macro scopes to search */
,exclude= /* Space delimited list of macro scopes to ignore */
,recurse= /* Used internally by the macro */
);
/*----------------------------------------------------------------------
Use SASHELP.VMACRO to pull value for a macro variable that is hidden by
local macro variables with the same name.
-----------------------------------------------------------------------
$Calls to: qlist.sas
-----------------------------------------------------------------------
Usage:
%* Allow GMV to serve as default value ;
%if %bquote(&MVAR) = %then %let mvar=%symget(mvar);
%* Allow both GMV and parameter style macro calls ;
%macro aesumm(dsnin=,dsnut=,sev=);
%local macro;
%let macro=&sysmacroname;
%if %bquote(&dsnin=) %then %let dsnin=%symget(dsnin,exclude=¯o);
%if %bquote(&dsnut=) %then %let dsnut=%symget(dsnut,exclude=¯o);
%if %bquote(&sev=) %then %let sev=%symget(sev,exclude=¯o);
....
%mend aesumm;
------------------------------------------------------------------------
Notes:
- Default to finding GLOBAL macro variable.
- Set SYSRC=1 when macro variable not found.
- Macro will call itself recursively to eliminate extra trailing blanks
pulled from SASHELP.VMACRO.
-----------------------------------------------------------------------
History:
2011/04/05 abernt Creation
----------------------------------------------------------------------*/
%local macro did rc where scope name value offset;
%let macro=&sysmacroname;
%if "&recurse"="0" %then %do;
%*----------------------------------------------------------------------
Open SASHELP.VMACRO and link dataset variables to macro variables.
If a match is found then expand VALUE as the result of the macro call.
Loop until all observations are read or OFFSET=0 indicates that the
start of another instance of the same variable has begun.
Close SASHELP.VMACRO.
-----------------------------------------------------------------------;
%let where=%upcase(name="&mvar" and scope="&include");
%let did=%sysfunc(open(sashelp.vmacro(where=(&where))));
%syscall set(did);
%if 0=%sysfunc(fetch(&did)) %then %do;
%do %until(%sysfunc(fetch(&did)) or 0=&offset);&value.%end;
%end;
%let rc=%sysfunc(close(&did));
%end;
%else %if %bquote(&mvar)^= %then %do;
%*----------------------------------------------------------------------
Set scope to GLOBAL when no criteria specified. Exclude this macro.
Open the SASHELP.VMACRO and link dataset variables to macro variables.
Fetch first observation to get SCOPE and NAME. Close SASHELP.VMACRO.
-----------------------------------------------------------------------;
%if %length(&include) %then %let where=scope in %qlist(%upcase(&include));
%else %if 0=%length(&exclude) %then %let where=scope='GLOBAL';
%else %let where=scope not in %qlist(%upcase(¯o &exclude));
%let where=name="%upcase(&mvar)" and &where;
%let did=%sysfunc(open(sashelp.vmacro(where=(&where))));
%syscall set(did);
%let rc=%sysfunc(fetch(&did));
%let did=%sysfunc(close(&did));
%if (0=&rc) %then %do;
%*----------------------------------------------------------------------
Found a variable so get value from recursive call to the macro.
Expand value as result of macro call.
-----------------------------------------------------------------------;
%let value=%¯o(&name,include=&scope,recurse=0);
&value.
%end;
%end;
%*----------------------------------------------------------------------
Set SYSRC=1 to indicate macro variable not found.
-----------------------------------------------------------------------;
%let sysrc=%eval(%bquote(&name)=);
%mend symget;