为什么不 %let 创建一个局部宏变量? [英] why doesn't %let create a local macro variable?

查看:42
本文介绍了为什么不 %let 创建一个局部宏变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为 %let 会创建一个 local 变量,如果在 %macro 内部使用...%修正

I always thought that %let creates a local variable if used inside of %macro . . . %mend

但是当我运行此代码时,SAS 日志显示 GLOBAL TESTVAR value1

But when I run this code , the SAS log shows GLOBAL TESTVAR value1

%let testVar = value2; 
%macro test; 
%let testVar = value1; 
%mend;   

%test 

%put _all_;

所以,我不明白为什么全局变量 testVar 的值变成了 value1 .我期待它保持不变 value2 .%macro 中的 %let 语句应该只影响本地符号表.

So, I can't understand why the value of the global variable testVar changed to value1 . I was expecting it to be unchanged value2 . The %let statement inside the %macro should have impacted ONLY the local symbol table.

SAS 文档说:

当宏处理器执行可以创建宏变量的宏程序语句时,如果没有同名的宏变量可用,宏处理器会在本地符号表中创建该变量

When the macro processor executes a macro program statement that can create a macro variable, the macro processor creates the variable in the local symbol table if no macro variable with the same name is available to it

推荐答案

关键是'如果没有同名的宏变量可用' - 在这种情况下,一个同名的宏变量是可用的,因为您已经将 testVar 定义为全局变量.

The key is 'if no macro variable with the same name is available to it' - in this case, a macro variable with the same name is available, because you've already defined testVar as a global.

您可以为其指定一个不与全局共享的名称,也可以将其显式声明为本地:

You can either give it a name that isn't shared with a global, or explicitly declare it as local:

%let testVar = value2; 
%macro test; 
    %local testVar;
    %let testVar = value1; 
%mend;   

%test 

这篇关于为什么不 %let 创建一个局部宏变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆