BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dustychair
Pyrite | Level 9

Hi all,

I want to sum rows until a new formid shows up and then write this sum in a new column along the same formid. I looked at some examples if "sum until" but not sure how to apply them to my case. I provided the data set that i have and want below. Thanks in advance for any contribution.

 

data have

formid max_score

aaa1 1

aaa1 0

aaa1 2

aaa2 1

aaa2 1

aaa2 3

want:

formid max_score total

aaa1 1 3

aaa1 0 3

aaa1 2 3

aaa2 1 5

aaa2 1 5 

aaa2 3 5

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Sum until implies a running total, but it seems you just want the total added to the data set. The easiest method is via SQL.

 

Otherwise, other approaches are outlined here:

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 

proc sql;
create table want as
select *, sum(max_score) as total
from have 
group by formid;
quit;

@dustychair wrote:

Hi all,

I want to sum rows until a new formid shows up and then write this sum in a new column along the same formid. I looked at some examples if "sum until" but not sure how to apply them to my case. I provided the data set that i have and want below. Thanks in advance for any contribution.

 

data have

formid max_score

aaa1 1

aaa1 0

aaa1 2

aaa2 1

aaa2 1

aaa2 3

want:

formid max_score total

aaa1 1 3

aaa1 0 3

aaa1 2 3

aaa2 1 5

aaa2 1 5 

aaa2 3 5


 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc sql;
    create table want as select 
        formid
        ,max_score
        ,sum(max_score) as total
    from have 
    group by formid;
quit;
--
Paige Miller
Reeza
Super User

Sum until implies a running total, but it seems you just want the total added to the data set. The easiest method is via SQL.

 

Otherwise, other approaches are outlined here:

https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas

 

proc sql;
create table want as
select *, sum(max_score) as total
from have 
group by formid;
quit;

@dustychair wrote:

Hi all,

I want to sum rows until a new formid shows up and then write this sum in a new column along the same formid. I looked at some examples if "sum until" but not sure how to apply them to my case. I provided the data set that i have and want below. Thanks in advance for any contribution.

 

data have

formid max_score

aaa1 1

aaa1 0

aaa1 2

aaa2 1

aaa2 1

aaa2 3

want:

formid max_score total

aaa1 1 3

aaa1 0 3

aaa1 2 3

aaa2 1 5

aaa2 1 5 

aaa2 3 5


 

PeterClemmensen
Tourmaline | Level 20
data have;
input formid $ max_score;
datalines;
aaa1 1
aaa1 0
aaa1 2
aaa2 1
aaa2 1
aaa2 3
;

data want;
   do until (last.formid);
      set have;
      by formid;
      total = sum(total, max_score);
   end;
   do until (last.formid);
      set have;
      by formid;
      output;
   end;
run;

 

Result:

 

formid  max_score  total
aaa1    1          3 
aaa1    0          3 
aaa1    2          3 
aaa2    1          5 
aaa2    1          5 
aaa2    3          5 
dustychair
Pyrite | Level 9

Thank you all!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1263 views
  • 0 likes
  • 4 in conversation