楼主: timj
7158 19

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

  • 0关注
  • 2粉丝

已卖:42份资源

本科生

6%

还不是VIP/贵宾

-

威望
0
论坛币
48 个
通用积分
6.2521
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
378 点
帖子
45
精华
0
在线时间
92 小时
注册时间
2005-8-3
最后登录
2016-4-16

楼主
timj 发表于 2011-9-15 08:18:12 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
1.The following SAS program is submitted:
%macro one(input);
%two
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date', '12SEP2008');
run;
%mend;
%let date = 31DEC2006;
%one(&date)
What is the result when the %PUT statement executes?
A.A macro variable DATE with the value 12SEP2008 is retrieved from the global symbol table.
B.A macro variable DATE with the value 31DEC2006 is retrieved from the global symbol table.
C.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the ONE macro.
D.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the TWO macro.

Answer: A

2.The following SAS program is submitted:
%macro one(input);
%two
%mend;
%macro two;
data _null_;
call symputx('date', '12SEP2008', 'L');
run;
%put the value is &date;
%mend;
%let date = 31DEC2006;
%one(&date)
What is the result when the %PUT statement executes?
A.A macro variable DATE with the value 12SEP2008 is retrieved from the global symbol table.
B.A macro variable DATE with the value 31DEC2006 is retrieved from the global symbol table.
C.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the ONE macro.D.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the TWO macro.

Answer: D

3.
%macro one(input);
%two
%mend;
%macro two;
data _null_;
call symputx('date', '12SEP2008', 'G');
run;
%put the value is &date;
%mend;
%let date = 31DEC2006;
%one(&date)
What is the result when the %PUT statement executes?
A.A macro variable DATE with the value 12SEP2008 is retrieved from the global symbol table.
B.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the ONE macro.
C.A macro variable DATE with the value 12SEP2008 is retrieved from the local symbol table for the TWO macro.
D.A macro variable DATE with the value 31DEC2006 is retrieved from the local symbol table for the TWO
macro.
Answer: A

小弟初学SAS,看不懂,请帮忙解释一下这几道题,多谢!!

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:宏程序 statement following Variable Statemen following result null 程序

沙发
leedx 发表于 2011-9-15 08:46:05
期待高人的回答~~~

藤椅
timj 发表于 2011-9-16 06:48:10
求高人指导!!

板凳
wzzlz117 发表于 2013-4-14 21:21:32
求解啊!!

报纸
jundal 发表于 2013-7-20 12:36:05
第一题:先查找是否存在该变量,查找顺序是由内而外,一旦找到该变量就进行更新。如果查找完都没找到,就在该语句所处最近的local table中创建该变量并赋值。
第二题和第三题:symputx中第三个参数是symbol table的意思,L代表local table,G代表global,F含义和第一题一样。

地板
sumanking 发表于 2014-12-3 04:56:19
第一题
我认为, 调用%one(&date)时,由于%one本身带local macro variable, 所以SAS 要先建个local symbol table来放local macro variable的值(假设这是新session, 所以以前没有local symbol table)。%one(&date)里面的%two就用到执行

%macro two;
data _null_;
call symput('date', '12SEP2008');
run;
%mend;

由于已经有了local symbol table, 所以call symput('date', '12SEP2008');就给date赋值12SEP2008并保存在local symbol table。(只有在local symbol table存在时才能使用call symput('date', '12SEP2008');)
最后%let date = 31DEC2006;是创建global macro variable 并把值 31DEC2006保存在global symbol table。
至此, 虽然有同名的两个变量date, 但一个保存在local symbol table,一个保存在global symbol table。

而%put the value is &date;是在macro definition 里面, 所以是local, 所以显示12SEP2008。

如果我说的对, 那个答案就是错的。

7
sumanking 发表于 2014-12-3 05:57:08
验证了一下,我是对的。在%let date = 31DEC2006;下面加%put the value is &date;
%macro one(date);
%two
%put the value is &date;
%mend;
%macro two;
data _null_;
call symput('date', '12SEP2008');
run;
%mend;
%let date = 31DEC2006;
%put the value is &date;
%one(&date)

结果是:
99   %let date = 31DEC2006;
100  %put the value is &date;
the value is 31DEC2006
101  %one(&date)

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


the value is 12SEP2008

8
sumanking 发表于 2014-12-3 06:14:21
对那个Rules for Creating and Updating Variables 不太明白, 就是那个查找顺序。在code 中已经明确表明变量是global 还是local了,直接放在相应的table里就可以了,为何还要先查local symbol table, 再查global , 都没有的话, 就在local symbol table 里创建? 如果我定义一个新的global macro variable,%LET a = b;, 在local symbol table和global symbol table里都没有,那按照这个RULES, 最后放到了local symbol table。 这跟是global variable完全不同啊。

9
teqel 发表于 2014-12-3 08:50:11
确实奇怪
%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

10
sumanking 发表于 2014-12-3 11:11:43
明白了。

1. call symput 必须放在data step 里用, 所以才有data _null_;
call symput('date', '12SEP2008');
run;。

2. 当此data step 不在一个macro definition里时,symput 创建的macro variable 是global, 放在global symbol table里
(此table有automatic macro variables, 永远不会空)。

3.  当此data step 在一个macro definition里时,symput 创建的macro variable 是locall, 放在the most local nonempty symbol table里。

4. 程序从%let date = 31DEC2006; %one(&date)开始。%let date = 31DEC2006; 创建global date 并放在global symbol table里。

5. 执行 %one(&date), 因为遇到macro parameter, 所以要创建一个local symbol table,把date 放进去并赋值NULL(此table不空了)。

6. 执行 %two (macro %one 调用 macro %two, 形成nested macros, 进而形成multiple local synbol tables), 因为遇到data _null_;
call symput('date', '12SEP2008');run;, 符合建立local symbol table 的两情况之一: when a request is made to create a local variable
during macro execution(另外一种情况是macro parameter), 所以要再创建一个local symbol table。 但这时这个table is empty,
不符合存放symput产生的date的nonempty要求. 而只能放在other the most local nonempty symbol table里, 这里就一个,只能放在
%one的local symbol table里并赋值12SEP2008。

7. %two 执行完后,它的那个空table即被删除。

8. %one里就剩%put the value is &date;了, 此date就是%one自己的table里的, 值是12SEP2008。%one 执行完后,其table也被删除。

9. 值为31DEC2006的date还在global symbol table里。要是你关了SAS软件, session 都结束了,那就什么table 都没了。

完毕!

最后回答我自己那个查找顺序的困惑。总的原则是global variable 放 global symbol table, local variable 放local
symbol table. 那个rules 是对%local %let statement的。但user defined macro variable 既可以放local 也可以放global, 详情自己弄吧。

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

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