全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
11426 7
2014-10-10
一、call execute
1、
call execute的意思就是“执行这段代码”,但是一个data步中间的call execute是不可能直接执行的,只有data步结束了这段代码才能执行。

2、
if ... then do; proc ... ; end;这样是无法执行的
if ... then call execute('...');else call execute('...') 这样是可以执行的

%if %then %do;这样的语句只能在宏语言中有效
if ... then call execute('...');这样的效果是一样的

3、
将多个call execute结合起来,可以根据数据集里面的数据产生一个更加复杂的过程步
eg.
根据一个数据集里的数据来对另一个数据集进行分类
obs sat_cutoff group_name
1        1200        Honor students
2        900        Regular track
3        300        Challenged
分类信息
复制代码
这一段代码和
复制代码
是一样的

4、call execute 中的过程步最好是能够用run来结尾;
call execute里面的宏是会立即执行的
eg.

call execute('%let pet=DOG;');
animal = symget('pet');
put animal; ->DOG

%let pet=CAT;
data _null_;
   if 5>4 then do;
   %let pet=DOG;
   end;
   if 5=4 then do;
   %let pet=RAT;
   end;
   animal = symget('pet');
   put animal; -> RAT
run;

所有的%let都会执行


%let pet=CAT;
data _null_;
   if 5>4 then do;
    call execute ('%let pet=DOG;');
   end;
   if 5=4 then do;
    call execute ('%let pet=RAT;');
   end;
   animal = symget('pet');
   put animal; -> DOG
run;

有趣的小例子
复制代码
Color began as blue.
Color ends up as red.
复制代码
Color began as blue.
Color ends up as blue.
因为宏会立即执行,而data步会在这个data步结束了才执行。

5、如何延迟call execute的执行,使过程步可以推迟执行
有时候我们不希望宏会马上执行,而是希望能够在data步之后再执行。
%let value = BEFORE;
data _null_;
        call execute("data _null_; call execute        ('%"||"let value = AFTER;');run;" );
        data_step_var=symget('value');
        put data_step_var; -> BEFORE
run;
%put Value is &value ..; ->Value is AFTER.
单独的将%let分开 或者用一个call execute 嵌套是不能使这个宏推迟执行的,只能在使用嵌套的同时将%let分开才可以。

另一个方法:
%let value=BEFORE;
data _null_;
        call execute('%nrstr(%let value=AFTER;)');
        data_step_var = symget('value');
        put data_step_var; ->BEFORE
run;
%put Value is &value..; ->Value is AFTER.

6、宏变量的解析
单引号是会阻止宏变量的解析的。call symput和call execute的组合会因为单引号和双引号而产生不同的结果:
复制代码
双引号的结果为 value is BEFORE;
单引号的结果为 value is AFTER;



二维码

扫码加我 拉你入群

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

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

全部回复
2014-10-10 18:07:28
感谢分享
二维码

扫码加我 拉你入群

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

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

2014-10-11 08:30:41
https://bbs.pinggu.org/thread-2377205-1-1.html
二维码

扫码加我 拉你入群

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

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

2016-8-22 21:14:20
感谢分享
二维码

扫码加我 拉你入群

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

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

2016-11-6 12:10:31
%let value = BEFORE;
data _null_;
        call symput('value','AFTER');
        call execute("%put Double Quotes: value is &value...;");
        call execute('%put Single Quotes: value is  &value...;');
run;

call execute() 中的代码涉及到宏的调用时,宏的执行时间有三种可能。即
1. 在DATA步编译时, 如 call execute("%test");
2. 在DATA步执行时,如 call execute( ‘%test’ );
3. 在DATA步执行后,如 call execute( '%'||"test');
二维码

扫码加我 拉你入群

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

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

2017-1-24 11:11:08
%let pet=CAT;
data _null_;
   if 5>4 then do;
   %let pet=DOG;
   end;
   if 5=4 then do;
   %let pet=RAT;
   end;
   animal = symget('pet');
   put animal; -> RAT
run;

所有的%let都会执行


关于这一段还没看明白,就是第二个5=4也会执行下面的%let吗,为什么?
二维码

扫码加我 拉你入群

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

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

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

说点什么

分享

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