代码错误,宏变量应该 return 列表中的第一个值,但它是 returning 最后一个值
Mistake in code, Macro variable should return first value in list but it is returning last value
我的代码有错误。它应该 return 列表中的第一个值,但 returning 最后一个值。我的代码
%macro r(one, two);
%local cnt num;
%let cnt=0;
%let num=0;
%do %while(%scan(&one,%eval(&cnt+1),%str( )) ne %str( ));
%let cnt = %eval(&cnt+1);
%if %upcase(%scan(&one,&cnt,%str( )))=%upcase(&two) %then
%let num=&cnt;
%end;
&num
%mend r;
option mlogic symbolgen;
%put b is at position %r(a b c b, b);
输出应该是 2 但我得到的输出是 4。谁能帮我找出我的错误。提前致谢。
您需要添加条件以在找到匹配项后退出 while
循环。否则它会一直寻找,这不是你想要的。
%macro r(one, two);
%local cnt num;
%let cnt=0;
%let num=0;
%do %while(&num.=0 and (%scan(&one,%eval(&cnt+1),%str( )) ne %str( )));
%let cnt = %eval(&cnt+1);
%if %upcase(%scan(&one,&cnt,%str( )))=%upcase(&two) %then
%let num=&cnt;
%end;
&num
%mend r;
option mlogic symbolgen;
%put b is at position %r(a b c b, b);
我的代码有错误。它应该 return 列表中的第一个值,但 returning 最后一个值。我的代码
%macro r(one, two);
%local cnt num;
%let cnt=0;
%let num=0;
%do %while(%scan(&one,%eval(&cnt+1),%str( )) ne %str( ));
%let cnt = %eval(&cnt+1);
%if %upcase(%scan(&one,&cnt,%str( )))=%upcase(&two) %then
%let num=&cnt;
%end;
&num
%mend r;
option mlogic symbolgen;
%put b is at position %r(a b c b, b);
输出应该是 2 但我得到的输出是 4。谁能帮我找出我的错误。提前致谢。
您需要添加条件以在找到匹配项后退出 while
循环。否则它会一直寻找,这不是你想要的。
%macro r(one, two);
%local cnt num;
%let cnt=0;
%let num=0;
%do %while(&num.=0 and (%scan(&one,%eval(&cnt+1),%str( )) ne %str( )));
%let cnt = %eval(&cnt+1);
%if %upcase(%scan(&one,&cnt,%str( )))=%upcase(&two) %then
%let num=&cnt;
%end;
&num
%mend r;
option mlogic symbolgen;
%put b is at position %r(a b c b, b);