返回 SAS 中列总和的数据集 [英] Return dataset of column sums in SAS

查看:33
本文介绍了返回 SAS 中列总和的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have many datasets for many years from 2001 to 2014 which look like the following. Each year is stored in one file, yXXXX.sas7bdat,

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

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

and I need to create a dataset where for each year we have the (weighted) sums of each of the X columns.

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

我很乐意将其实现到宏中,但我不确定一种隔离列总和的方法,以及一种将结果附加在一起的有效方法(proc append?)

I would be happy to implement this into a macro but I am unsure of a way to isolate column sums, and also an efficient way to attach results together (proc append?)

包括一次尝试.

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

推荐答案

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

This is probably what I'd do, append all the data sets together and run one proc means. You didn't mention how big the data sets are, but I'm assuming smaller data.

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;

这篇关于返回 SAS 中列总和的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆