我有一个数据集,当前月份和上个月有2列。我只需要对这两列执行求和运算。问题是每月更新列的名称更改。如何在SAS中自动执行此过程?
当前数据集
ID Sep Oct
1 23 12
2 31 19
3 37 21
下个月数据集
ID 10月10月 1 17 21
2 12 23
3 55 21
如何每月自动对两列进行求和?
答案 0 :(得分:2)
如果总是有三列,第一列是ID,第二列是第3个是你想要求和的值,使用数组......
data mysum ; set mydata ; array n{*} _NUMERIC_ ; /* ID VAR1 VAR2 */ /* sum 2nd & 3rd elements */ month_sum = sum(n{2},n{3}) ; run ;
答案 1 :(得分:0)
如果您要汇总列:
proc sql noprint;
select
cat('sum(', trim(name), ') as month', put(monotonic(), 1. -L)) into :sum_statement separated by ', '
from dictionary.columns
where libname = 'WORK'
and memname = 'SOMETABLE'
and upcase(name) ne 'ID'
;
quit;
%put &sum_statement;
proc sql;
create table sum as
select &sum_statement from WORK.SOMETABLE
;
quit;