sas累积返回多个变量

时间:2015-10-06 15:11:45

标签: loops sas

我一直在运行一个datastep来计算变量X1的累积积,给它一个起始值1.然而,我无法使用%do循环运行几个变量。我在下面提供了两个代码。帮助赞赏。

DATA HAVE;
INFILE DATALINES DSD;
INPUT X1 X2 X3 YEAR;
DATALINES;
0.99,0.98, 0.97,2005
0.975,0.96, 0.923,2006
0.983,0.934, 0.93,2007
0.978,0.93, 0.887,2008
;

data working;
set have;
retain R1 1;
S1=R1*X1;
output;
R1=S1;
drop S1;
run;


%macro macro5(j);
data notworking;
set have;
%DO i=1 %to &j;
retain R&i 1;
S&i=R&i*X&i;
output;
R&i=S&i;
drop S&i;
%end;
run;
%mend;
%macro5(3);

1 个答案:

答案 0 :(得分:0)

使用数组,而不是宏循环。

DATA HAVE;
INFILE DATALINES DSD;
INPUT X1 X2 X3 YEAR;
DATALINES;
0.99,0.98, 0.97,2005
0.975,0.96, 0.923,2006
0.983,0.934, 0.93,2007
0.978,0.93, 0.887,2008
;

data want;
set have;
array x(3) x1-x3;
array r(3) r1-r3;

retain r1-r3 1;

output;

do i=1 to 3;
r(i)=x(i)*r(i);
end;

drop i;
run;