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!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.