楼主: dollarkfl
12248 12

[问答] 在SAS宏语言中用do循环时,为什么有时候加不加%产生的效果一样 [推广有奖]

11
farmman60 发表于 2014-3-26 09:13:44 |只看作者 |坛友微信交流群
You have a good question. Sometimes I am also confused about it. Maybe it is not right, my understandings are:

The %do-end% statements are valid only inside a macro definition, for example

%macro test;
  %do year=2011 %to 2013;
    %do month=1 %to 12;
          %put &year.&month;
        %end;
    %end;
%mend test;
%test

%macro print;
   %do year=1980 %to 1994;
           proc print data=sashelp.retail;
           where year=&year;
           run;
   %end;
run;
%mend print;

%print

you could not replace %do-%end with do-end, such as:
%macro test;
  do year=2011 to 2013;
    do month=1 to 12;
          %put &year.&month;
        end;
    end;
%mend test;
%test

So %do loop could be used everywhere in a macro definition, do loop could be only used in DATA STEP within a macro definition, in some conditions it is allowed to use either like your examples, in some conditions like follows, %do-%end could not be replaced by do-end, so the best way is to use %do-%end in macro program.

data class;
set sashelp.class;
run;

%macro make_ind(dsn,cat);
proc sort data=&dsn out=sorted;
by &cat;
run;
data _null_;
set sorted end=eof;
by &cat;
if first.&cat then do;
tot+1;
call symputx("&cat.ind"||compress(tot),compress(&cat));
end;
if eof then call symputx('tot',tot);
run;

data &dsn._ind;
set &dsn;
%do i=1 %to &tot;
if &cat="&&&cat.ind&i" then &&&cat.ind&i =1;
else &&&cat.ind&i =0;
%end;
run;
%mend make_ind;

%make_ind(class,sex)


使用道具

12
dollarkfl 发表于 2014-3-26 18:35:59 |只看作者 |坛友微信交流群
谢谢

使用道具

13
sglsh007 发表于 2020-12-8 16:14:29 |只看作者 |坛友微信交流群
高手!

使用道具

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

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

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

GMT+8, 2024-4-30 18:52