-3

Can we call GLOBAL MACRO variables which is created by % let, % do , callsymput, sql into clause ,

macro parameters,

please tell me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • What to you mean by CALL? Do you mean expand the value of them? – Tom Apr 02 '18 at 18:13
  • Scoping: http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1b76sxg9dbcyrn1l5age5j5nvgw.htm&docsetVersion=9.4&locale=en – Tom Apr 02 '18 at 18:27

1 Answers1

1

For the sake of discussion the highest scope is the global scope

The macro variable utilized at time of ampersand or superq resolution is the current or nearest higher scope. If you require a same named macro variable value from a scope higher than the default you must query it from dictionary table (SASHELP.VMACRO)

When you plan on populating a global macro variable from code executed within a macro invocation, declare the macro variable using the %GLOBAL statement;

The depth of a macro's execution can be known using the macro function %SYSMEXECDEPTH. There are numerous other macro functions for 'reflection' of the macro system, mostly the SYS* and SYM* functions found in SAS documentation.

An example. The zoot macro populates global macro variable age, even when zoot is invoked in a macro execution depth.

options nocenter;

%let MY_MACRO_VAR = global;

%macro foo;
  %local MY_MACRO_VAR;
  %let MY_MACRO_VAR = local_1;

  %put &=MY_MACRO_VAR;

  options nolabel;
  title "&SYSMACRONAME %nrstr(%SYSMEXECDEPTH=)%SYSMEXECDEPTH";
  proc sql;
    select * from dictionary.macros
    where name = 'MY_MACRO_VAR';
  quit;
  options label;

  %bar;
%mend;

%macro bar;
  %local MY_MACRO_VAR;
  %let MY_MACRO_VAR = local_%SYSMEXECDEPTH;

  %put &=MY_MACRO_VAR;

  options nolabel;
  title "&SYSMACRONAME %nrstr(%SYSMEXECDEPTH=)%SYSMEXECDEPTH";
  proc sql;
    select * from dictionary.macros
    where name = 'MY_MACRO_VAR';
  quit;
  options label;

  %put &SYSMACRONAME %nrstr(%SYSMEXECDEPTH=)%SYSMEXECDEPTH;
  %put _user_;  

  %if %SYSMEXECDEPTH=2 %then %bar;

  %zoot
%mend;

%macro zoot;
  %global my_global;
  proc sql noprint;
    select age into :my_global from sashelp.class where name = 'Jane';
  quit;
%mend;

%foo;

%put _user_;

The scope of other interactions with the macro system such as EXECUTE, SYMGET or RESOLVE can become rather subtle.

Richard
  • 25,390
  • 3
  • 25
  • 38