You can do it by joining the same table in the "from" statement, but each table is grouped differently
proc sql;
select
t1.P,
t1.L,
t1.Exp,
t2.TotalExp,
t1.TotalExpL
from
(select P, L, Exp, sum(Exp) as TotalExpL from t group by P, L) t1
inner join
(select P, sum(Exp) as TotalExp from t group by P) t2
on t1.P=t2.P
;
quit;
You can do it by joining the same table in the "from" statement, but each table is grouped differently
proc sql;
select
t1.P,
t1.L,
t1.Exp,
t2.TotalExp,
t1.TotalExpL
from
(select P, L, Exp, sum(Exp) as TotalExpL from t group by P, L) t1
inner join
(select P, sum(Exp) as TotalExp from t group by P) t2
on t1.P=t2.P
;
quit;
Please use the "little running man" to post your code, and do some formatting on the code. This spaghetti is nigh unreadable.
Thanks. It worked for this case. If becomes more co
proc sql;
select t1.P,
t1.L,
t1.Exp,
t1.cd,
t2.TotalExp,
t1.TotalExpCd
t1.TotalExpL
from
(((select P, L, Exp, sum(Exp) as TotalExpL from t group by P, L) t1
(select P, L, Exp, sum(Exp) as TotalExpCd from t group by P, CD)) t1)
inner join (select P, sum(Exp) as TotalExp from t group by P) t2
on t1.P=t2.P ;
quit;
mplicated with one more variable for sorting (see below), how can we proceed?
Your ERROR is caused by an unneeded and invalid comma, and probably a missing QUIT:
proc sql;
SELECT
P, L, Exp,
((select sum(exp) FROM t group by P ) as TotalExp ),
((Select sum(exp) FROM t group by P,L ) as TotalExpL), /* this comma is not needed */
FROM t;
run; /* SQL statements are executed immediately, so no RUN is needed */
/* use a QUIT statement to end the SQL procedure */
It does not work
proc sql;
SELECT
P, L, Exp,
((select sum(exp) FROM t group by P ) as TotalExp),
((Select sum(exp) FROM t group by P,L ) as TotalExpL)
FROM t;
quit;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.