BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alphabeta
Calcite | Level 5
Hi,

I have a question of summing counts by group while maintaining them individually in the original dataset.

For example:
obs group count -> obs group sum_count
1   1        10           1    1        25
2   1        15           2    1        25
3   2         5            3    2         5

I know there is a way in sas to sum counts by group that gets:
group count
1        25
2        5    

But if I create a new dateset using this output and merge back with my original dateset, the combined dataset would only read up to ''n (group)" instead of my original "N(observations)'.

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 Smiley Sad

Thanks!!

1 ACCEPTED SOLUTION

Accepted Solutions
dcruik
Lapis Lazuli | Level 10

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!

View solution in original post

4 REPLIES 4
dcruik
Lapis Lazuli | Level 10

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!

Tom
Super User Tom
Super User

PROC SQL let's you do that.

create table want as

select *,sum(count) as sum_count

from have

group by group

;

AskoLötjönen
Quartz | Level 8

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;

alphabeta
Calcite | Level 5

Thanks to you all. I really appreciate your information!

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2197 views
  • 0 likes
  • 4 in conversation