BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sir_Highbury
Quartz | Level 8

Dear experts,

 

given the following input data:

 

data inputs x var1 var2 var3 var4 var5;
datalines;
20 5 2 4 5 4
25 12 56 13 44 4
20 5 2 4 5 4
25 12 56 13 44 4
20 5 2 4 5 4
25 12 56 13 44 4
. 2 5 6 5 4
;

I get the following output:

proc sql; create table output as select
x,
sum(var1) as var1,
sum(var2) as var2,
sum(var3) as var3,
sum(var4) as var3,
sum(var5) as var4
from inputs group by 1 ;quit;

 

how is possible to get the same result but saying: do the sum from var1 to var_n_?

Thank in advance, SH.

1 ACCEPTED SOLUTION

Accepted Solutions
4 REPLIES 4
Reeza
Super User

Either a macro or proc means. 

Proc means is much easier. 

Kurt_Bremser
Super User

Use a variable list in the var statement of proc means/summary:

var var1-var5;

If number of variables is stored in a macro variable N, replace var5 with var&N.

Sir_Highbury
Quartz | Level 8

hervorragend! 😉

overmar
Obsidian | Level 7

Or if you are going to stay in proc sql

 

%macro summing(n= );

proc sql; create table output as select
x, %do i = 1 %to &n; sum(var&i) as var&i

%if &i = &n %then %do; from inputs group by 1; %end;

%else %do; , %end;

quit;

%mend;

%summing(n=5);

 

Also it would probably make sense to use select distinct rather than just select, and i'm not quite sure why you are using group by 1

rather than something like group by x (as this is the only variable which are leaving in the output table). But that's besides the point. Hope this helps.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2187 views
  • 0 likes
  • 4 in conversation