SAS宏变量更改+数组索引
这与以下问题相关:SAS宏变量更改。
下面的代码解释了这个问题:
%macro test (arg=);
options mlogic mprint symbolgen;
array arraytwo [%EVAL(&arg+1)] _temporary_;
sum=0;
%do i = 1 %to %EVAL(&arg+1);
sum=sum+&i;
arraytwo[&i]=sum;
%end;
return=arraytwo[&arg+1];
%mend test;
/* This is ok */
data dat1;
%test(arg=9);
run;
data dat2;
input M;
cards;
5
6
7
;
run;
/* This give an error= A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: M+1 */
data dat3;
set dat2;
%test(arg=M);
run;
那么问题是为什么它在上次测试中会出现错误?谢谢。
This is related to this question: SAS macro variable change.
The code below explains the problem:
%macro test (arg=);
options mlogic mprint symbolgen;
array arraytwo [%EVAL(&arg+1)] _temporary_;
sum=0;
%do i = 1 %to %EVAL(&arg+1);
sum=sum+&i;
arraytwo[&i]=sum;
%end;
return=arraytwo[&arg+1];
%mend test;
/* This is ok */
data dat1;
%test(arg=9);
run;
data dat2;
input M;
cards;
5
6
7
;
run;
/* This give an error= A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: M+1 */
data dat3;
set dat2;
%test(arg=M);
run;
So the question is why does it bug in the last test? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您碰巧使用 SAS 9.2 或更高版本,您可能需要查看 proc fcmp 来创建一个函数来执行此操作。
如果将其编写为函数而不是宏,则可以传入将解析为数值的数据集变量 - 或直接传递数值。例如,尝试以下代码:
If you happen to be using SAS 9.2 or later you might want to look at proc fcmp to create a function to do this.
If you write it as a function instead of a macro, you can pass in data set variables that would resolve to numeric values - or pass numeric values directly. For example, try this code:
我不得不说我不完全确定你想做什么;但这能给你你想要的结果吗?上面代码的问题在于您尝试组合数据集变量和宏变量的方式——它并不像人们希望的那么容易做到......
I have to say I'm not entirely sure what you're trying to do; but does this give you the results you're looking for? The problem with your code above is the way you are trying to combine dataset variables and macro variables-- it isn't as easy to do as one might hope...