全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
1202 2
2014-03-08
只是想到这个问题,求教给个思路;
首先举个栗子
=======================================
%macro circle;
%do i=1 %to 100-2;
        %do j=i+1 %to 100-1;
                %do k=j+1 %to 100;
                %woe;
                %end;
        %end;
%end;
run;

%mend circle;
%circle;
=======================================
/*第一层是100-(层数-1) 第二层是100-(层数-2)...依此类推...直到100*/
也许不放在%macro里面,对应do  to(不要在意这些细节);
这段程序视为3层循环步;
现在我希望做一个1层循环步、一个2层循环步...一直到做一个8层循环步,请教如何实现,如何控制循环步的层数?
二维码

扫码加我 拉你入群

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

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

全部回复
2014-3-9 11:38:51
First, the loop has 8 levels and the total calls will be roughly 100**8/2 which is a huge number. It almost is impossible to make your problem doable. You need to reconsider/formulate your problem. It is good tip of always to aviod to have nested loops!

Second, it is possible to control the loop levels based on a pre-defined parameter.

In general, you cannot control the level within a data step. This is simple because SAS data step is a compiling language. You cannot hide the loop levels from the SAS compiler. But you can use SAS macro language to generate the loop levels as desired, the desired codes then pass to SAS compiler and executed.

Similarly, you cannot control macro loop levels with  SAS macros. Pretty Sad!

Here is a old way you can bypass it(generate SAS macro language with a data step). The same logic can be applied to generate data step codes. The old trick always works!

n=levels
stop=100 in your problem.

Do not try it with large number of n and stop. Here will take long to to even to compile it  and take forever to run it.

data _null_;
  file 'c:\temp\testmacro.sas';
length loopvar loopstart loopend $30;
  n=3;
  stop=6;
  ***********************;
  put '%macro circle(dummy);';

  do loop=1 to n;
    loopvar=catt('_i_',loop);
    if loop=1 then loopstart='1';
        else loopstart=catt('&_i_',loop-1,'+1');
        loopend=stop-n+loop;
    put '%do ' loopvar '=%eval(' loopstart ') %to ' loopend ';';


   end;
/*    put '*%woe;';*/
        put 'proc print data=sashelp.class(obs=1);run;';
   
    do loop=1 to n;
    put '%end; ' ;
   end;
   put '%mend circle;';
run;
options source2 mprint;
%include 'c:\temp\testmacro.sas';

%circle(dummy)
;;;;;
二维码

扫码加我 拉你入群

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

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

2014-3-9 12:39:08
复制代码
二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

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