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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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