- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I used %let to define local macro in %macro statement, but it was gone way outside of the macro. Because I would like to use macro variables generated in %macro outside of it, I want to use global macro. But, replacing %global with %let doesn't work for me. Do they work differently?
Relatedly, can I simply re-define local macro variables to global macro ones?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Please show the code you have tried. This will make it easier to explain why it's not working.
It's a good idea to always use the %LOCAL or %GLOBAL statement to explicitly declare the scope of macro variables (there are rare exceptions to this rule).
Yes you can use the %GLOBAL statement in a macro to create a global macro variable, for example:
%macro try() ;
%global x ;
%let x=1 ;
%mend try ;
%try()
%put &=x ;
No, if you have a local macro variable, you cannot use the %GLOBAL statement to redefine it as global. The below log shows the error that will be generated if you try:
615 %macro try() ; 616 %local x ; 617 %let x=1 ; 618 %global x ; 619 %let x=1 ; 620 %mend try ; NOTE: The macro TRY completed compilation without errors. 15 instructions 236 bytes. 621 622 %try() ERROR: Attempt to %GLOBAL a name (X) which exists in a local environment.
HTH,
-Q.
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Note that you use %LET to assign values to macro variables, not to create LOCAL or GLOBAL macro variables.
I suspect you are just confused because if the macro variable you are assigning a value to does not already exist then it will be created as LOCAL.
Try this
%macro mymacro;
%if not %symexist(mymvar) then %global mymvar;
%let mymvar=Hi there;
%mend mymacro;
%mymacro;
%put My Macro Varaible = &mymvar;