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!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

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