I have a question of summing counts by group while maintaining them individually in the original dataset.
Is there any other ways of getting my original dateset to my goal?
I couldn't find a direct sas command for doing the equivalent sum_count stata did
Thanks!!
Try the following code using BY processing:
data have;
input obs group count;
datalines;
1 1 10
2 1 15
3 2 5
;
run;
data want;
do until (last.group);
set have;
by group;
sum_count=sum(count,sum_count); end;
do until (last.group);
set have;
by group;
output; end;
run;
The first do until sum's all counts within each by group and the last do until outputs all observations from the original data set with the sum_count field.
Hope this helps!
Try the following code using BY processing:
data have;
input obs group count;
datalines;
1 1 10
2 1 15
3 2 5
;
run;
data want;
do until (last.group);
set have;
by group;
sum_count=sum(count,sum_count); end;
do until (last.group);
set have;
by group;
output; end;
run;
The first do until sum's all counts within each by group and the last do until outputs all observations from the original data set with the sum_count field.
Hope this helps!
PROC SQL let's you do that.
create table want as
select *,sum(count) as sum_count
from have
group by group
;
data have;
input obs group count ;
cards;
1 1 10
2 1 15
3 2 5
;
run;
proc sql;
create table want as
select group,
count,
sum(count) as N_group
from have
group by group;
quit;
Thanks to you all. I really appreciate your information!
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.
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.
Ready to level-up your skills? Choose your own adventure.