Solved
New Contributor
Posts: 2

# How to sum columns

Hi,

I have a dataset like

C1    C2    C3    ...    C1000

1        1      1                1

2        2      2                2

3        3      3                3

I want to sum the columns but I don't want to mention them in the code (because there are so much),

in order to get a table like this

C1    C2    C3    ...    C1000

6      6        6                    6

Thanks!

Accepted Solutions
Solution
‎07-04-2012 07:06 PM
Valued Guide
Posts: 765

## Re: How to sum columns

hi ... if all you want is the sum of all the numeric variables in the data set, why not PROC SUMMARY ...

proc summary data=have;

var _numeric_;

output out=want (drop=_ sum=;

run;

Message was edited by: Mike Zdeb ... the NWAY in the original posting  (proc summary data=have nway is not needed since there's no CLASS statement used in the PROC

All Replies
Posts: 5,535

## Re: How to sum columns

Use arrays, like this :

data test;
array c{1000};
do i = 1 to 3;
do j = 1 to 1000;
c{j} = ranuni(-1);
end;
output;
end;
drop i j;
run;

data want(keep=c;
array c{1000};
array s{1000} (1000*0);
do until(endTest);
set test end=endTest;
do j = 1 to 1000;
s{j} + c{j};
end;
end;
do j = 1 to 1000;
c{j} = s{j};
end;
run;

PG

PG
Posts: 3,167

## Re: How to sum columns

How about something just learned form Ksharp, as long as you don't have so many variables that 32K of macro variable length can be exhausted.

data h;

input C1    C2    C3 C4;

cards;

1        1      1                1

2        2      2                2

3        3      3                3

;

RUN;

proc sql;

select cats('sum(',NAME,') AS SUM_',NAME,'') into :lst separated by ',' from dictionary.columns where libname='WORK' AND MEMNAME='H';quit;

proc sql;

SELECT &LST from h;

quit;

Haikuo

Super Contributor
Posts: 1,636

## Re: How to sum columns

or

data have;

input c1-c6;

cards;

1 1 1 1 1 1

2 2 2 2 2 2

3 4 5 6 7 8

;

proc sql noprint;;

select cats('total_',name,'+',name) into :list separated by ';'from dictionary.columns

where libname='WORK' and memname='HAVE'; /* libname,memname have to be capital letters */

quit;

%put &list;

data want;

set have end=last;

&list;

if last then output;

keep total:;

proc print;run;

Obs    total_c1    total_c2    total_c3    total_c4    total_c5    total_c6

1         6            7           8             9              10             11

Linlin

Solution
‎07-04-2012 07:06 PM
Valued Guide
Posts: 765

## Re: How to sum columns

hi ... if all you want is the sum of all the numeric variables in the data set, why not PROC SUMMARY ...

proc summary data=have;

var _numeric_;

output out=want (drop=_ sum=;

run;

Message was edited by: Mike Zdeb ... the NWAY in the original posting  (proc summary data=have nway is not needed since there's no CLASS statement used in the PROC

Super Contributor
Posts: 1,636

## Re: How to sum columns

Hi Mike,

Your code is really short and nice!

New Contributor
Posts: 2

## Re: How to sum columns

Thanks MikeZdeb ! I really love you!

And thanks ALL OF YOU! You are great!

🔒 This topic is solved and locked.