gpt4 book ai didi

sas - 限制 %do %while 迭代

转载 作者:行者123 更新时间:2023-12-01 16:27:05 29 4
gpt4 key购买 nike

我想在宏函数中运行 while/until 循环,并限制其最大迭代次数。我找到了如何在“通常”sas 中执行此操作:

 data dataset;
do i=1 to 10 until(condition); /*10 iterations max */
/* stuff */
end;
run;

但是如果我在宏函数中尝试它:

 %macro mf;
data dataset;
%do i=1 %to 10 %until(nrow(X)>10); /*10 iterations max */
/* stuff */
%end;
run;
%mend;

%mf;

我收到这些错误:

ERROR: Improper use of macro reserved word until.
ERROR: A dummy macro will be compiled.
ERROR: Required operator not found in expression: 10 %until(nrow(X)>10)
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro MF will stop executing.

限制宏函数中循环迭代的正确方法是什么?

如果您想测试想法,这里有一个数据集:

DATA dataset;
input X Y Z;
cards;
10 0 20
50 20 60
90 60 30
run;

最佳答案

以下是您可以使用的示例:

%macro mf;
%let i=0;
%do %until(&onechar=e or &i=10);
%let i=%eval(&i+1);
%let onechar=%substr(abcdefghij,&i,1);
%end;
%put onechar=&onechar;
%put i=&i;
%mend mf;
%mf;

如果找到“e”或i=10,宏循环就会停止。

关于sas - 限制 %do %while 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23678034/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com