楼主: harlon1976
11952 14

如何退出SAS宏循环 [推广有奖]

11
ademons 发表于 2010-2-16 22:16:50
%return是直接终止宏了,而不是跳出循环。

%%mend ml(n,m,zb);
多了一个%。



data a;
        do i=1 to &n;
                x=uniform(0);
                fi=arcos(-1);
                y=sin(2*fi*x);
                output;
                keep y x fi i;
        end;


%do j=2 %to &m;

12
soporaeternus 发表于 2010-2-16 22:29:06
  1. data a;

  2. do i=0 to 9 by 1;

  3. do x=i+0.995 to i+1.005 by 0.001;

  4. output;

  5. end;

  6. end;

  7. drop i;

  8. run;



  9. %MACRO TST(min_n,max_n,max_range);

  10. %let cls=&min_n;

  11. %let rc=1;

  12. %do %while (%eval(&rc));

  13. proc fastclus data=a MAXCLUSTERS=&cls out=b_&cls;run;quit;

  14. proc means noprint data=b_&cls;
  15. var x; by cluster;
  16. output out=menx&cls range=rx;

  17. quit;

  18. proc sort data=menx&cls;by rx;run;quit;
  19. data _null_;
  20.         set menx&cls;by rx;
  21.         if _N_=1 then do;
  22.                 call symput("min_range_now",rx);
  23.         end;
  24. run;


  25. %put NOW MIN_RANGE IS &min_range_now N_CLS IS &cls;

  26. %if &min_range_now<=&max_range %then %do;

  27. %let rc=0;

  28. %end;

  29. %else %do;

  30. %let cls=%eval(&cls+1);

  31. %if &cls>&max_n %then %let rc=0;

  32. %end;

  33. %end;

  34. %MEND TST;

  35. %TST(1,150,0.01);
复制代码

但愿可以
Let them be hard, but never unjust

13
bobguy 发表于 2010-2-17 08:53:58
harlon1976 发表于 2010-2-16 20:15
9楼的同志的程序是不是应该可以理解为退出%do,等价地退出宏。我仿照这个理解,编写如下程序:
%macro ml(n,m,zb);
data a;
do i=1 to &n;
x=uniform(0);
fi=arcos(-1);
y=sin(2*fi*x);
output;
keep y x fi i;
end;
%do j=2 %to &m;
proc fastclus data=a out=Clust&j maxclusters=&j noprint;
var x;
proc sort out=flei&j;by cluster;
proc means noprint data=flei&j;
var x; by cluster;
output out=menx&j(keep=rx) range=rx;
proc means data=menx&j noprint;
var rx;
output out=big&j max=zuizhi;
data pb&j;
set  big&j ;
%if zuizhi le &zb %then %return;
%end;
%%mend ml(n,m,zb);
%ml(200,15,0.4);run;
但提交出现问题,问题就在%if 这里,不知高手是否给予修正呢?
谢谢。
上面程序是确定一个最好的分类数,使得各个类内的变量的极差最大值小于事先给定的值,此时退出循环,得到最终的分类结果。
You are confused with macro programming and data step programming/SAS programming. A macro needs to compile before executed. A macro is nothing but a text generator.  Based on a data step variable to determine if exit-timing a macro is not working. Here is a workable idea.

Write two macro
1) A macro check the value to determine if it is needed to call another macro. In your case it will be in a data step.
2) Calculating macro in your case will be a proc fastclus, ect.

HTH

14
bobguy 发表于 2010-2-20 23:56:31
10# harlon1976


Based on what I understand your pseudo-macro codes I modify as below.
%let n=200;
data a;
do i=1 to &n;
x=uniform(0);
fi=arcos(-1);
y=sin(2*fi*x);
output;
keep y x fi i;
end;
run;
%macro fast(a, j);
proc fastclus data=a out=Clust&j maxclusters=&j noprint;
var x;
run;
proc sort out=flei&j;by cluster;
run;
proc means noprint data=flei&j;
var x; by cluster;
output out=menx&j(keep=rx) range=rx;
run;
proc means data=menx&j noprint;
var rx;
output out=big&j max=zuizhi;
run;
%mend;
%macro ml(m,zb);
%let trigger=0;
%do j=2 %to &m;
%if &trigger=0 %then %fast(a,&j);
data _null_;
set big&j;
if zuizhi le &zb then call symputx('trigger',1);
stop;
run;
%put >>>>>>>loop number j=&j;
%if &trigger=1 %then %return;
%if &j=&m %then %return;
%end;
%%mend ml;

%ml(20,0.1);

15
xktse 发表于 2012-4-12 15:01:41
%GOTO 语句可以实现 参考如下:

%LET VAR=%STR(A B C);

%MACRO TEST;

%DO I=1 %TO 3;
        %LET T=%SCAN(&VAR.,&I);
        %PUT &T.;
        %IF "&T."="B" %THEN %DO;
                %LET C="HOLLE WORLD";
                %GOTO QUIT;
        %END;
%END;

%QUIT:

%MEND;       
       
%TEST;

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-26 15:45