返回SAS中列和的数据集

时间:2017-01-16 19:50:42

标签: sas

从2001年到2014年,我有很多年的数据集,如下所示。每年存储在一个文件yXXXX.sas7bdat

ID Weight X1 X2 X3
1  100    1  2  4
2  300    4  3  4

我需要创建一个数据集,其中每年我们都有X列的(加权)总和。

X1 X2 X3 Year
10 20 30 2014
40 15 20 2013

我很乐意将其实现为宏,但我不确定是否有一种隔离列总和的方法,也是一种将结果附加到一起的有效方法(proc追加?)

编辑:包括尝试。

%macro final_dataset;

%do i = 2001 %to 2014;

/*Code here which enables me to get the column sums I am interested in.*/

proc means data = y&i;
 weight = weight;
 X1 = SUM X1;
 X2 = SUM X2;
 X3 = SUM X3;
 OUTPUT OUT = sums&i;
run;

data final;
 set final sums&i;
run;

%end;
%mend;

编辑:另一次尝试。

%macro final_dataset;

%do i = 2001 %to 2014;

/*Code here which enables me to get the column sums I am interested in.*/

proc means data = y&i SUM;
 weight = weight;
 var X1 X2 X3;
 OUTPUT OUT = sums&i;
run;

data final;
 set final sums&i;
run;

%end;
%mend;

编辑:最终。

%macro final_dataset;

%do i = 2001 %to 2014;

/*Code here which enables me to get the column sums I am interested in.*/

proc means data = y&i SUM NOPRINT;
 weight = weight;
 var X1 X2 X3;
 OUTPUT OUT = sums&i sum(X1 X2 X3) = X1 X2 X3;
run;

data final;
 set final sums&i;
run;

%end;
%mend;

1 个答案:

答案 0 :(得分:1)

这可能就是我所做的,将所有数据集附加在一起并运行一个proc方法。你没有提到数据集有多大,但我假设数据较小。

data combined;
length source year $50.;
set y2001-y2014 indsname=source;
*you can tweak this variable so it looks how you want it to;
year=source;
run;


proc means data=combined noprint nway;
class year;
var x1 x2 x3;
output out=want sum= ;
run;