1

I wish to use select into: to create a global variable in a macro module.

%macro example();
   proc sql noprint;
      select x into:DDD from aaa;
   quit;
%mend;

The problem is I wish to create a global variable DDD, in this example we can only get the local variable DDD. Thanks. Wayne.

andrey_sz
  • 751
  • 1
  • 13
  • 29
Wayne
  • 33
  • 1
  • 6

2 Answers2

1

I don't think you can control the scope within Proc SQL. You an use call symputx in a data step to specify the scope.

However, macro variables created in open code, via Proc SQL, are global by default. Within a macro it will be local.

As noted in the comments you can also explicitly declare a macro variable as global using

 %global macro_variable_name;
Reeza
  • 20,510
  • 4
  • 21
  • 38
  • I agree that code doesn't need to be in a macro, but if it were prefixed by this: `%global DDD;`, that'd make DDD global. – mjsqu Jul 04 '16 at 05:04
  • That's true. Depending on the dynamic nature of a macro that may or may not work. – Reeza Jul 04 '16 at 05:05
0

As @Reeza pointed out you can simply specify that you want the macro-variable global at the beginning of your macro with the %global DDD; statement.

Just one focus point:

sometimes you can recall a macro from inside another macro. In this case you could have the same macro-variable pre-existing as local in the outer-macro. Recalling the inner-macro with inside a %global statement you could encounter an error due to the conflict between the global and local definition of the same macro-variable (ERROR: Attempt to %GLOBAL a name (XXX) which exists in a local environment.).

In this case you can:

  • pre-define the macro-variable as global also in the outer-macro
  • pre-define the macro-variable as local (or simply give it a (non-)value with a %let, sql into: or call symput) in the outer-macro avoiding the %global statement in the inner-macro

depending on the macro-variable visibility requirement you have.

DaBigNikoladze
  • 661
  • 3
  • 9