楼主: timj
7159 19

[问答] 请教3道SAS adv考试的宏程序的题 [推广有奖]

11
teqel 发表于 2014-12-3 11:40:26 来自手机
sumanking 发表于 2014-12-3 11:11
明白了。

1. call symput 必须放在data step 里用, 所以才有data _null_;
其他好理解,但是为什么two里面的date值是global的值?

12
sumanking 发表于 2014-12-3 11:53:47
teqel 发表于 2014-12-3 11:40
其他好理解,但是为什么two里面的date值是global的值?
? two is a macro, 里面的date值是local的值.

13
teqel 发表于 2014-12-3 12:16:46
sumanking 发表于 2014-12-3 11:53
? two is a macro, 里面的date值是local的值.
看我的结果:
the value two is 31DEC2006
the value one is 12SEP2008
the value global is 31DEC2006
不知道在你机器上是不是也是这样?

14
sumanking 发表于 2014-12-3 13:17:06
%macro one(date);
%two
%put the value one is &date;
%mend;
%macro two;
data _null_;
call symput('date', '12SEP2008');
%put the value two is &date;
run;
%mend;
%let date = 31DEC2006;
%one(&date)
%put the value global is &date;

显示:
the value two is 31DEC2006
the value one is 12SEP2008
the value global is 31DEC2006

这涉及到rules for creating and updating variables.
以上程序首先创建global variable date, and put it in the global symbol table.
two里%put the value two is &date;先查找自己的local symbol table(不查别人的), 因是空的,所以NO.
按rules再查global symbol table, YES.
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

15
teqel 发表于 2014-12-3 13:21:31
sumanking 发表于 2014-12-3 13:17
%macro one(date);
%two
%put the value one is &date;
这个告诉我们,要想使用symput的值,必须要到外面一层(One)用

16
sumanking 发表于 2014-12-3 13:28:53
teqel 发表于 2014-12-3 13:21
这个告诉我们,要想使用symput的值,必须要到外面一层(One)用

one有自己的local symbol table, and has a variable date with value 12SEP2008 in the table.

17
sumanking 发表于 2014-12-4 09:10:01
第一题答案是对的。
开始时,先创建一个global variable date放在global synbol table里, 赋值31DEC2006。two 创建的local variable date由于two的local symbol table是空,
所以不能放。而这时one自己的local symbol table里只有一个variable 是input。本题关键就在这里。按照Rules for Creating and Updating Variables, macro processor
要先检查两个这local symbol table
里是否有同名的variable, 如果有就把值替换掉,如果没有则到 global symbol table里去查看是否有相同名字的variable, (这时如还没有, 就转身回到
local symbol table里创建新的variable名),这时正好在 global symbol table里有同名的date(值是31DEC2006),就用新的值12SEP2008替换掉旧值31DEC2006。
所以选A

18
sumanking 发表于 2014-12-7 02:40:57
刚刚考完adv, 83分。两小时62题, 65分过。不都是选择题, 还有两个填空题,一个是在macro definition里用%put 显示自定义的global 变量,另一个是在程序里填写ALL或 CORR option。感觉跟网上的题重复的不多, 但大概考试内容差不多,网上的那些题都弄懂了,过这考试没问题。 看到网上有人4天过adv考试,真不知是我辈太弱还是他们根本就是来自外太空文明。

19
1029614045 在职认证  发表于 2015-5-5 15:30:47
感谢!!!

20
lleiou 发表于 2016-10-31 05:33:12
根据网上的一篇小论文[1], 我总结了一下: CALL SYMPUT 定义的宏到底是 local 还是 global 呢?
* If defined outside the macro def: Global
* Defined inside the macro def + macro has an empty local symbol table: Global
* Defined inside the macro def + macro has non-empty local symbol table: Local
* Explicitly define them as GLOBAL or LOCAL: It will be the same as what you defined

所以这道题可以解决: 因为 Local Symbol Table 是空的, 所以 SYMPUT 创建的 date 是一个 global 的 macro, 其值为 12SEP2008, 并且把最开始那个也叫做 date 的 global macro 覆盖了.
-
[1] 论文链接加不上, 直接把 URL 写在这里: http://www.lexjansen.com/phuse/2009/po/PO15.pdf
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-9 23:19