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!
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.