这应该是一个简单的问题,但我没有弄清楚......我想通过proc univariate
如下方式获得许多变量的平均值和中位数。M_
手动将平均值和MD_
中值添加到所有变量中确实非常耗时。我想知道是否有任何简单的方法,例如数组这样做?非常感谢!
代码:
data old;
input year type A1 A2 A3 A4 A5;
datalines;
2000 1 1 2 3 4 5
2000 1 2 3 4 5 6
2000 2 3 4 5 6 7
2000 2 4 5 6 7 8
2001 1 5 6 7 8 9
2001 1 6 7 8 9 10
2001 1 7 8 9 10 11
2001 2 8 9 10 11 12
2001 2 9 10 11 12 13
2001 2 10 11 12 13 14
2002 1 11 12 13 14 15
2002 1 12 13 14 15 16
2002 1 13 14 15 16 17
2002 2 14 15 16 17 18
2002 2 15 16 17 18 19
2002 2 16 17 18 19 20
run;
proc univariate data=old noprint;
var A1 A2 A3 A4 A5;
by year type;
output out=new
mean=M_A1 M_A2 M_A3 M_A4 M_A5
median=MD_A1 MD_A2 MD_A3 MD_A4 MD_A5;
run;
预期的演示代码:
%let varlist = A1 A2 A3 A4 A5;
array vars (*) &varlist;
proc univariate data=old noprint;
var &vars(*);
by year type;
output out=new
mean=M_&vars(*)
median=MD_&vars(*);
run;
使用 proc sql 正确的代码
%macro uni;
%let varlist='A1','A2','A3','A4','A5';
%let vars=A1 A2 A3 A4 A5;
proc sql;
select cats('M_',name) into :meannamelist separated by ' '
from dictionary.columns
where libname='WORK' and memname='OLD' and name in (&varlist);
select cats('MD_',name) into :mediannamelist separated by ' '
from dictionary.columns
where libname='WORK' and memname='OLD' and name in (&varlist);
quit;
proc univariate data=old;
var &vars;
by year type;
output out=new
mean=&meannamelist
median=&mediannamelist;
run;
%mend uni;
options mprint;
%uni;