SAS 宏全局范围

发布于 2024-07-14 11:55:22 字数 168 浏览 8 评论 0原文

有没有一种简单的方法可以使在宏全局范围内创建的所有宏变量都处于作用域中?

IE:

%macro x;  
 %global _all_;  * ??? ;  
 %let x=1;  
 %let y=1;  
 %let z=1;  
%mend;

is there a short way to make ALL macro variables created inside a macro global in scope?

ie:

%macro x;  
 %global _all_;  * ??? ;  
 %let x=1;  
 %let y=1;  
 %let z=1;  
%mend;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-07-21 11:55:22

我能想到的唯一方法是使用宏代替,而无需提前将每个宏声明为全局宏,然后执行 %let 语句%let 语句。

在下面的代码中,我创建了一个名为 %mylet 的宏,其唯一目的是使用我作为参数传递的名称和值来创建全局变量。 然后,我在想要定义全局变量的地方使用这个宏来代替 %let 。

例如。

%global myvar;
%let myvar=2;

会成为...

%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;

The only way I can think of to do this without having to declare each macro as global ahead of time and then do a %let statement is to use a macro in place of the %let statement.

In the code below, I create a macro called %mylet which has as its sole purpose the task of creating a global variable with the name and value I pass as arguments. I then use this macro in place of %let everywhere I want global variables defined.

eg.

%global myvar;
%let myvar=2;

would become...

%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;
孤檠 2024-07-21 11:55:22

如果您在开放代码中创建宏变量,则宏变量将自动添加到全局宏符号表中:

%let x=1;  
%let y=1;  
%let z=1; 

这将在您创建更大的宏之前

%macro x;
  <code here>
%mend x;

另一种选择是使用数据步骤:

data _null_;
    set LIB.DSET;
    x = 1;
    call symput('x',x);
run;

If you create the macro variable within open code the macro variable will automatically be added to the global macro symbol table:

%let x=1;  
%let y=1;  
%let z=1; 

This would be before you create the larger macro:

%macro x;
  <code here>
%mend x;

Another alternative would be to create the variables using a data step:

data _null_;
    set LIB.DSET;
    x = 1;
    call symput('x',x);
run;
隐诗 2024-07-21 11:55:22

如果您在数据步骤中创建宏变量,并且该数据步骤位于宏内部,则默认情况下,创建的宏变量将在该宏的范围内是本地的。

用于

call symputx('macvar',macval,'g'); 

创建全局宏变量。

If you create macro variables within a data-step, and that data-step is inside a macro, the macro variables created will be, by default, local in scope to that macro.

Use

call symputx('macvar',macval,'g'); 

to create global macro variables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文