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

Hi to all!

I have to find sums in one table but based on different Grouping be each time (relevant Partition by in SQL). How can I do it?

In this code I want to find Sum(Exp) group by P, sum(Exp) group by P, L and sum(MV) group by P, Cd). How many inner joins I need?

proc sql;
select * 

from 
(((select P, L, Exp, MV, sum(Exp) as TotalExpPL,  from t group by P, L) t2
inner join 
(select P, sum(Exp) as TotalExpP from t group by P) t1
on t1.P=t2.P)
((select  P, Cd, sum(MV) as TotalMVPCD from t group by P, Cd) t3
inner join on t1.P=t3.P))
;
quit;

1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @cmemtsa 

Why not using a PROC MEANS instead?

You can then filter on the _type_ variable to get the desired results.

 

proc means data=t noprint;
	class p L Cd;
	var Exp MV;
	output out=want sum=/autoname;
run;

 

View solution in original post

1 REPLY 1
ed_sas_member
Meteorite | Level 14

Hi @cmemtsa 

Why not using a PROC MEANS instead?

You can then filter on the _type_ variable to get the desired results.

 

proc means data=t noprint;
	class p L Cd;
	var Exp MV;
	output out=want sum=/autoname;
run;

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 675 views
  • 3 likes
  • 2 in conversation