简体   繁体   English

proc sql中的sas宏

[英]sas macro in proc sql

i have a problem that probably very simple but i can't figured out. 我有一个可能很简单的问题,但我不知道。

i have columns name1 c1 c2 c3 c4 .... c8 i have to sum by names these columns and make a new data. 我有列name1 c1 c2 c3 c4 .... c8我必须按名称对这些列进行求和并生成新数据。

Here is the code that i wrote but it didnt work. 这是我写的代码,但是没有用。

Could you please help me? 请你帮助我好吗?

PROC SQL;
   CREATE TABLE WORK.data1 AS 
   SELECT t1.name1, 

%let k=8
%macro test;
%do i=0 %to &k;
%sysfunc(SUM(C&i)) AS C&i;
%END;
%mend test;
%test;
      FROM WORK.data t1
      GROUP BY t1.name1,
QUIT;
data data;
input name1 $ c1 c2 c3;
datalines4;
a 1 2 3
a 1 2 3
b 1 2 3
b 1 2 3
;;;;
run;

%macro test(k=);
  PROC SQL;
   CREATE TABLE WORK.data1 AS 
   SELECT t1.name1 

%do i=1 %to &k.;
   , SUM(C&i.) AS C&i.
%END;

  FROM WORK.data t1
  GROUP BY t1.name1;
QUIT;

%mend test;
%test(k=3);

This seems a convoluted way of achieving your result, PROC SUMMARY can do it much easier utilising the colon modifier to sum all variables beginning with C. The code below runs off the dataset created by @Mike and produces identical output. 这似乎是一种复杂的实现结果的方法,PROC Summary可以使用冒号修饰符对以C开头的所有变量求和,从而更加轻松。下面的代码运行@Mike创建的数据集并产生相同的输出。

proc summary data=data nway;
class name1;
var c: ;
output out=want (drop=_:) sum=;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM