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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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