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

Hi,

 

I have quite a simple question. My data is:

 

v1v2v3
100200250

 

Now, I want to calculate the cumulative sum over the row to get:

 

c1c2c3
100300550

 

How do I do that?

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

You can use ARRAYs.

 

data want;
    set have;
    array v v1-v3;
    array c c1-c3;
    do i=1 to dim(v);
        if i=1 then c(i)=v(i);
        else c(i)=sum(c(i-1),v(i));
    end;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

You can use ARRAYs.

 

data want;
    set have;
    array v v1-v3;
    array c c1-c3;
    do i=1 to dim(v);
        if i=1 then c(i)=v(i);
        else c(i)=sum(c(i-1),v(i));
    end;
run;
--
Paige Miller
MarkusB
Calcite | Level 5

Great, thank you all for the solutions!

novinosrin
Tourmaline | Level 20

data have;
input v1	v2	v3;
cards;
100	200	250
;

data want;
 set have;
 array t v:;
 do _n_=2 to dim(t);
  t(_n_)=sum(t(_n_),t(_n_-1));
 end;
run;
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
  • 3 replies
  • 1177 views
  • 0 likes
  • 3 in conversation