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!

 

I want to update a column with subtotals on another column based on grouping. Do I have to do an inner join in a subquery

data T;
input ID $ L M;
Datalines;
B 1 10 
B 1 20 
C 1 30 
C 2 40 
D 1 50 
D 1 60 
D 2 70 
;

proc sql;
alter table T add M1 numeric;
update T 
set M1 = 
(select sum(M) from T  group by ID, L )
;
quit;
 

?

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Rhodochrosite | Level 12

This might do the trick

proc sql;
	select id, l, m, sum(m) as m1
	from t
	group by id, l
	order by id, l
	;
quit;

View solution in original post

9 REPLIES 9
AhmedAl_Attar
Rhodochrosite | Level 12

This might do the trick

proc sql;
	select id, l, m, sum(m) as m1
	from t
	group by id, l
	order by id, l
	;
quit;
cmemtsa
Quartz | Level 8

That works but as this is a part  of the code, is there a solution with Update?

AhmedAl_Attar
Rhodochrosite | Level 12

Try this

proc sql;
    /* Create Temporary aggregates table */
	create table sums as
	select id, l, sum(m) as m1
	from t
	group by id, l
	order by id, l;

/* Alter the table */ alter table T add M1 num;
/* Update the t table */ update t set m1 = (select s.m1 from sums s where t.id=s.id and t.l=s.l);
/* Drop the temporary aggregates table */ drop table sums; quit;

Hope this helps,

Ahmed

cmemtsa
Quartz | Level 8

I also thought of that solution but I wanted to ask whether there was a shorter version through creation of mirror table and inner join within the subquery.

 

I will go with that solution since it's not feasible otherwise. Thanks.

Jagadishkatam
Amethyst | Level 16

please try below code

 

data T;
input ID $ L M;
Datalines;
B 1 10 
B 1 20 
C 1 30 
C 2 40 
D 1 50 
D 1 60 
D 2 70 
;

proc sql;
create table want as select ID, L, M, sum(M) as sumM from T  group by ID, L ;
quit;
Thanks,
Jag
ed_sas_member
Meteorite | Level 14

Hi @cmemtsa 

You cannot update the T table by using an inner query that uses this table as it is already in use.

I would suggest that you create another table:

proc sql;
	create table T2 as
	select *, sum(M) as M1
	from T
	group by 
	ID, L;
quit;

Best,

cmemtsa
Quartz | Level 8

Thank you but is there a way to use Update with subquery and though a mirror table?

Ksharp
Super User

If you really want UPDATE .

 

data T;
input ID $ L M;
Datalines;
B 1 10 
B 1 20 
C 1 30 
C 2 40 
D 1 50 
D 1 60 
D 2 70 
;

proc sql;
create table _t as select * from t;
alter table T add M1 numeric;
update T 
set M1 = 
(select sum(M) from _T  where id=t.id and l=t.l )
;
quit;
cmemtsa
Quartz | Level 8
Thanks! This is exactly what I wanted.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 961 views
  • 6 likes
  • 5 in conversation