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!
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.