## how to get the variable that is based on cumulative sum of pervious rows and current row?

Frequent Contributor
Posts: 133

# how to get the variable that is based on cumulative sum of pervious rows and current row?

hi all,

data t;

input m;

cards;

5

4

11

;

data t2;

set t;

k=m+;

output;

run;

I want result like this:

m k

5  5

4  9

11 20

Super Contributor
Posts: 1,636

## how to get the variable that is based on cumulative sum of pervious rows and current row?

data t;

input m;

cards;

5

4

11

;

data want;

set t;

retain k;

k+m;

run;

proc print;run;

Linlin

Frequent Contributor
Posts: 133

## how to get the variable that is based on cumulative sum of pervious rows and current row?

what is the purpose of retain statement?

Is K initially assume as zero?

Should I do following if I want to initial k value other than zero?

data want;

set t;

k=2.4;

retain k;

k+m;

run;

Valued Guide
Posts: 765

## how to get the variable that is based on cumulative sum of pervious rows and current row?

hi ... if you don't want to set an initial value, you can skip the RETAIN statement since a SUM statement (K+M) implies retain and starts with an initial value of 0

if you want to set an initial value ... RETAIN K 2.4;

Discussion stats
• 3 replies
• 163 views
• 0 likes
• 3 in conversation