gpt4 book ai didi

SAS宏变量变化+数组索引

转载 作者:行者123 更新时间:2023-12-01 11:06:21 27 4
gpt4 key购买 nike

这与这个问题有关:SAS macro variable change .

下面的代码解释了这个问题:

%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;

那么问题来了,为什么上次测试会出bug?谢谢。

最佳答案

如果您碰巧使用的是 SAS 9.2 或更高版本,您可能需要查看 proc fcmp 来创建一个函数来执行此操作。

如果将其编写为函数而不是宏,则可以传入可解析为数值的数据集变量,或直接传递数值。例如,试试这段代码:

proc fcmp outlib=work.funcs.simple;
function sumloop(iter);
x=1;
do i=1 to iter+1;
x+i;
end;
return(x);
endsub;
run;

/* point to the location the function was saved in */
option cmplib=work.funcs;

data _null_;
input M;
y=sumloop(M); /* data set variable */
z=sumloop(9); /* static numeric value */
put M= @7 y= @14 z= @20 ;
cards;
1
2
3
4
5
6
7
8
9
;
run;

/* My log looks like this:

14 data _null_;
15 input M;
16 y=sumloop(M); /* data set variable */
17 z=sumloop(9); /* static numeric value */
18 put M= @7 y= @14 z= @20 ;
19 cards;

M=1 y=3 z=55
M=2 y=6 z=55
M=3 y=10 z=55
M=4 y=15 z=55
M=5 y=21 z=55
M=6 y=28 z=55
M=7 y=36 z=55
M=8 y=45 z=55
M=9 y=55 z=55
*/

关于SAS宏变量变化+数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5456675/

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