楼主: intheangel
9871 7

[学习分享] call execute学习小结分享 [推广有奖]

  • 3关注
  • 89粉丝

VIP

副教授

25%

还不是VIP/贵宾

-

威望
1
论坛币
49842 个
通用积分
6.1883
学术水平
152 点
热心指数
168 点
信用等级
122 点
经验
12931 点
帖子
480
精华
0
在线时间
475 小时
注册时间
2013-11-22
最后登录
2023-6-28

初级热心勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
一、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
分类信息
  1. data _null_;
  2. set cutoffs end=lastone;
  3. if _n_=1 then call execute("data student_groups;
  4.                              set all_students;
  5.                              length group $ 14;");
  6. call execute("if score>="||put(sat_cutoff,best16.));
  7. call execute("then group='"||group_name||"';");
  8. if lastone=0 then call execute("else");
  9. else call execute("run;");
  10. run;
复制代码
这一段代码和
  1. data student_groups;
  2. set all_students;
  3. length group $14;
  4. if score>=1200 then group='Honor students';
  5. else if score>=900 then group='Regular track';
  6. else if score>=300 then group='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;

有趣的小例子
  1. %macro mymac;
  2.    %let color=blue;
  3.    %put Color began as &color...;
  4.    data _null_;
  5.     call symput('color','red');
  6.    run;
  7.    %put Color ends up as &color...;
  8. %mend mymac;

  9. %mymac
复制代码
Color began as blue.
Color ends up as red.
  1. data _null_;
  2. call execute('%mymac');
  3. run;
复制代码
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的组合会因为单引号和双引号而产生不同的结果:
  1. %let value = BEFORE;
  2. data _null_;
  3.         call symput('value','AFTER');
  4.         call execute("%put Double Quotes: value is &value...;");
  5.         call execute('%put Single Quotes: value is  &value...;');
  6. run;
复制代码
双引号的结果为 value is BEFORE;
单引号的结果为 value is AFTER;



二维码

扫码加我 拉你入群

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

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

关键词:EXECUTE Cute call ECU cut 分类信息 null

已有 4 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
李会超 + 100 精彩帖子
bitcoin + 1 + 1 + 1 观点有启发
webgu + 80 + 80 + 3 + 3 + 3 辛苦!
dxystata + 60 + 2 + 2 精彩帖子

总评分: 经验 + 240  论坛币 + 80  学术水平 + 6  热心指数 + 6  信用等级 + 4   查看全部评分

本帖被以下文库推荐

我是一只瘦瘦的小猪~~~
╭︿︿︿╮
{/-◎◎-/}
( (oo) )
  ︶︶︶
沙发
aongao 发表于 2014-10-10 18:07:28 |只看作者 |坛友微信交流群
感谢分享

使用道具

藤椅
pobel 在职认证  发表于 2014-10-11 08:30:41 |只看作者 |坛友微信交流群
https://bbs.pinggu.org/thread-2377205-1-1.html

使用道具

板凳
420948492 发表于 2016-8-22 21:14:20 |只看作者 |坛友微信交流群
感谢分享

使用道具

报纸
cylo 发表于 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吗,为什么?

使用道具

7
lipeng135 发表于 2023-3-23 15:41:36 |只看作者 |坛友微信交流群
感谢楼主分享好资源

使用道具

8
舍身卫道 发表于 2023-3-24 12:05:51 |只看作者 |坛友微信交流群
谢谢楼主分享

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 12:31