全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
3400 10
2013-01-10
%let rc = Begin;
%macro test;
data out;
set sashelp.prdsale nobs = totalobs;
if totalobs > 10 then do;
%let rc = high;
end;
else do;
%let rc = low;
end;
run;
%mend;
%let rc = Before Execution;
%test;
%put &rc;
为什么rc的值是low????
二维码

扫码加我 拉你入群

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

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

全部回复
2013-1-10 21:24:29
2. Given the non-indexed SAS data set TEMP:
TEMP
X Y
P 52
P 45
A 13
A 56
R 34
R 12
R 78
The following SAS program is submitted:
proc print data = temp;
run;
Which BY statement completes the program, creates a listing report that is grouped by X, and completes without errors?
(A) by X;
(B) by X grouped;
(C) by X notsorted;
(D) by descending X;
这道题大家有什么想法,答案是c,能否解释一下!
二维码

扫码加我 拉你入群

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

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

2013-1-11 00:05:42
昨天学习了kittyforever的关于macro的帖子,我现在这边也是班门弄斧的回答一下

%let rc = Begin;   (1)
%macro test;
data out;
set sashelp.prdsale nobs = totalobs;
if totalobs > 10 then do;
%let rc = high; (2a)
end;
else do;
%let rc = low; (2b)
end;
run;
%mend;
%let rc = Before Execution; (3)
%test;
%put &rc;


其中出现了3个给RC赋值的语句,其中1 和3 在外部,属于global赋值,2a和2b在marco内部,属于local赋值,因为macro中间有内部赋值,这样会覆盖掉1的外部赋值,此时执行到3之前,rc已经被赋值成low了,但是经过3之后,rc又被赋值成 Before Execution, 但是3之后又进行运算了macro,rc又被赋值回low,所以最后put rc就出来low了。



所以,也可以这样进行推演

%let rc = Begin;   (1)
%macro test;
data out;
set sashelp.prdsale nobs = totalobs;
if totalobs > 10 then do;
%let rc = high; (2a)
end;
else do;
%let rc = low; (2b)
end;
run;
%mend;
%let rc = Before Execution; (3)
%put &rc; (4)
%test;
%put &rc; (5)
我又新加了一步(4),这样(4)里面rc的值就为Before Execution, 而(5)的rc值还是被赋回low

再看
%let rc = Begin;   (1)
%macro test (rc=start) ******;
data out;
set sashelp.prdsale nobs = totalobs;
if totalobs > 10 then do;
%let rc = high; (2a)
end;
else do;
%let rc = low; (2b)
end;
run;
%mend;
%let rc = Before Execution; (3)
%put &rc;(4)
%test (rc=end)*****;
%put &rc; (5)
因为在marco中间对RC赋值,这样就表明这个rc是一个local赋值,不会影响到外部的rc的global赋值,而(3)的外部赋值将要覆盖掉(1)的外部赋值,所以最后 (4)或者(5)的赋值都将会是Before execution。希望能够帮助到你



二维码

扫码加我 拉你入群

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

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

2013-1-11 01:01:58
%let rc = Begin;
%macro test;
data out;
   set sashelp.prdsale nobs = totalobs;
  if totalobs > 10 then  call symput('rc', 'high');
    else   call symput('rc', 'low');
run;
%mend test;
%let rc = Before Execution;
%test;
%put &rc;

output in the log
%put &rc;
high
二维码

扫码加我 拉你入群

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

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

2013-1-11 03:59:27
or there is another case

%let rc = Begin;%macro test;
data out;
set sashelp.prdsale nobs = totalobs;
%if totalobs > 10 %then %do;
%let rc = high;
%end;
%else %do;
%let rc = low;
%end;

run;
%mend;
%let rc = Before Execution;
%test;
%put &rc;

二维码

扫码加我 拉你入群

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

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

2013-1-11 07:50:54
Your macro is equivalent to,

%macro test;
%let rc = high;
%let rc = low;
data out;
set sashelp.prdsale nobs = totalobs;
if totalobs > 10 then do;
end;
else do;
end;
run;
%mend;

What you need to understand is the difference between SAS macro language and other SAS language. They are very different.

二维码

扫码加我 拉你入群

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

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

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群