Hi,
I have quite a simple question. My data is:
v1 | v2 | v3 |
100 | 200 | 250 |
Now, I want to calculate the cumulative sum over the row to get:
c1 | c2 | c3 |
100 | 300 | 550 |
How do I do that?
Any help would be greatly appreciated!
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;
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;
Great, thank you all for the solutions!
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.