当你有一张桌子时;
somefield1 somefield2 lastterm1 lastterm2
(10)(20)(1)( - )
(20)(40)(1)(2)
(30)(50)( - )(2)
并尝试对每个术语进行分组,是否有更简单的方法来做而不是;
PROC SQL;
CREATE TABLE WORK.LAST_TERM1 AS
SELECT T1.LASTTERM1,
(MEAN(t1.'somefield1'n)) FORMAT=COMMAX14.2 AS 'MEAN_of_somefield1'n,
(MEAN(t1.'somefield2'n)) FORMAT=COMMAX14.2 AS 'MEAN_of_somefield2'n
FROM WORK.MAIN_TABLE t1
GROUP BY t1.LASTTERM1;
RUN;
PROC SQL;
CREATE TABLE WORK.LAST_TERM2 AS
SELECT T1.LASTTERM2,
(MEAN(t1.'somefield1'n)) FORMAT=COMMAX14.2 AS 'MEAN_of_somefield1'n,
(MEAN(t1.'somefield2'n)) FORMAT=COMMAX14.2 AS 'MEAN_of_somefield2'n
FROM WORK.MAIN_TABLE t1
GROUP BY t1.LASTTERM2;
RUN;
两个字段和两个字词不是问题,但如果您有20个字词和200个字段,则此代码无效。
我尝试过但没有像1到20那样做,并创建所有表格。
有没有办法做到这一点?
答案 0 :(得分:0)
我认为你正在寻找proc手段。您可以使用sas-macro替换输出表和类变量的名称来循环遍历分组变量列表(即lastterm1,lastterm2),并且应该拥有您需要的所有内容。
这是一个简单的宏,它将为last_term1 - last_term20执行此操作。它有点不清楚你想要什么,所以这是一个基本的广义形式。
%macro looper();
%do i = 1 %to 20;
proc means data=WORK.MAIN_TABLE;
var somefield1 somefield2;
class lastterm&i.;
output out=WORK.LAST_TERM&i.
mean(somefield1) = MEAN_of_somefield1
mean(somefield2) = MEAN_of_somefield2;
run;
%end;
%mend;
%looper();