BookmarkSubscribeRSS Feed
novinosrin
Tourmaline | Level 20

Proc sql

 


data have;
input OBS 	ID	Grouper	Paid	VAR2 	VAR3;
drop obs;
cards;
1	111	 .	100	150	200
2	111	0	100	150	200
3	111	 .	100	150	200
4	122	 .	100	150	200
5	122	7	100	150	200
6	123	1	100	150	200
7	125	 .	100	150	200
8	125	 .	100	150	200
9	125	 .	100	150	200
10	125	 .	100	150	200
;
proc sql;
create table want as
select id,grouper,sum(paid) as s1,sum(var2) as s2, sum(var3) as s3
from 
(select *, max( Grouper ne .) as k from have group by id having k=1)
group by id
having grouper>.
union all
select id,grouper,paid as s1, var2 as s2, var3 as s3
from (select *, max( Grouper ne .) as k from have group by id having k=0)
order by id;
quit;
Jagadishkatam
Amethyst | Level 16
proc sort data=have;
by id descending grouper ;
run;

data want(keep=id grouper2 paid2 rename=(grouper2=grouper paid2=paid));
set have;
by id descending grouper ;
retain grouper2 paid2;
if first.id then grouper2=.;
if grouper ne . then grouper2=grouper;
if first.id  then paid2=.;
if grouper2 ne . then paid2+paid;
if paid2 eq . then paid2=paid;
if last.id and grouper2 ne . then output;
if grouper2 eq . then output;
run;


Thanks,
Jag
novinosrin
Tourmaline | Level 20
data have;
input OBS 	ID	Grouper	Paid;
drop obs;
cards;
1	111	 .	100
2	111	0	100
3	111	 .	100
4	122	 .	100
5	122	7	100
6	123	1	100
7	125	 .	100
8	125	 .	100
9	125	 .	100
10	125	 .	100
;
data want;
do until(last.id);
merge have have(keep=id grouper where=(grouper ne .) in=in2);
by id;
if in2 then do; 
sum=sum(paid,sum);
if Grouper>. then _g=Grouper;
end;
else  do; sum=paid;output;end;
end;
if in2 then  do;
Grouper=_g;
output;
end;
drop _:;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 17 replies
  • 1900 views
  • 2 likes
  • 5 in conversation