Pregunta

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.

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;
¿Fue útil?

Solución

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

Otros consejos

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 data=data nway;
class name1;
var c: ;
output out=want (drop=_:) sum=;
run;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top