All*_*owe 3 global sas sas-macro
是否有一种简短的方法可以在范围内的宏全局内创建所有宏变量?
即:
%macro x;
%global _all_; * ??? ;
%let x=1;
%let y=1;
%let z=1;
%mend;
Run Code Online (Sandbox Code Playgroud)
我可以想到这样做的唯一方法,而不必提前将每个宏声明为全局,然后执行%let语句就是使用宏来代替%let语句.
在下面的代码中,我创建了一个名为%mylet的宏,其唯一目的是创建一个全局变量,其名称和值作为参数传递.然后我使用这个宏代替%let,我想要定义全局变量.
例如.
%global myvar;
%let myvar=2;
Run Code Online (Sandbox Code Playgroud)
会成为...
%mylet(myvar,2);
/* Define a macro to declare variables as global */
%macro mylet(var,value);
%global &var;
%let &var.= &value ;
%mend;
/* Test macro */
%macro test;
%mylet(myvar,2);
%mylet(myvar2,12);
%mylet(myvar3,'string');
/* see that they are global inside the macro */
title "Macro scope inside test macro";
proc sql;
select *
from dictionary.macros
where name in('MYVAR','MYVAR2','MYVAR3');
quit;
%mend;
%test;
/* Check to see if they are still global outside the macro */
title "Macro scope outside test macro";
proc sql;
select *
from dictionary.macros
where name in('MYVAR','MYVAR2','MYVAR3');
quit;
Run Code Online (Sandbox Code Playgroud)