楼主: firefox29
1255 2

能否控制循环步层数 [推广有奖]

  • 11关注
  • 1粉丝

博士生

28%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
0.2251
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
3694 点
帖子
131
精华
0
在线时间
305 小时
注册时间
2012-1-23
最后登录
2023-6-20

楼主
firefox29 发表于 2014-3-8 14:26:30 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
只是想到这个问题,求教给个思路;
首先举个栗子
=======================================
%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层循环步,请教如何实现,如何控制循环步的层数?
二维码

扫码加我 拉你入群

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

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

关键词:circle Macro CIRC 如何实现 End 程序

沙发
bobguy 发表于 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)
;;;;;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
firefox29 + 1 + 1 + 1 逻辑清晰,精彩帖子!!

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

藤椅
yongyitian 发表于 2014-3-9 12:39:08
  1. /* this is a datastep version */

  2. data _null_;
  3.    nloop=3;
  4.    start_loop = 1;
  5.    end_loop = 10;

  6.    length loops $200 loop_ind $8 ends $200;
  7.   do i = 1 to nloop;
  8.      loop_ind = cats('a', put(i,1.));
  9.      loops = catx(' ', loops, 'do', loop_ind, '=', start_loop+i-1, 'to', end_loop-nloop+i, ';');
  10.      ends=catx(' ', ends, 'end;');
  11.   end;

  12.     call symput('loops', %str(loops));
  13.     call symput('ends', %str(ends));
  14. run;

  15. %put This is the looping code:  %superq(loops)  %superq(ends);

  16. data _null_;
  17.     i=1;
  18.     &loops;
  19.       i++1;
  20.       put @5 i=   @15 'aaa';
  21.     &ends;
  22. run;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
firefox29 + 2 + 2 + 2 精彩帖子

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-4 00:50