C语言变量控制循环层数,能否控制循环步层数

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)

;;;;;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值