## How to sum element from n and below

Solved
Super Contributor
Posts: 328

# How to sum element from n and below

data CP;

input t ;

cards;

2

3

2

4

I want  to create variable sum where sum add the number from n and below like this:

2  Sum=2+2+3+2+4

2  Sum=  2+3+2+4

3 Sum=  3+2+4

Sum=  2+4

4  Sum=4

2) And how Do I do this. I want to add 2, 2(2) t the terms in t as I go dowm. Creating variable m.

data AA;

input t;

cards ;

1

1

1

1

to

data AA;

input t m;

cards ;

1  1+2

1   1+2(2)

1  1+3(2)

1  1+4(2)

Thanks

Accepted Solutions
Solution
‎01-09-2014 08:25 PM
PROC Star
Posts: 8,164

## Re: How to sum element from n and below

I'm confused!  I thought the code I suggested DID preserve the order in both cases.

All Replies
PROC Star
Posts: 8,164

## Re: How to sum element from n and below

data CP;

input t;

recnum=_n_;

cards;

2

2

3

2

4

;

proc sort data=cp out=aa;

by descending recnum;

run;

data aa;

set aa;

sum+t;

run;

proc sort data=aa (drop=recnum);

by descending sum;

run;

data bb;

input t;

m=t+_n_*2;

cards ;

1

1

1

1

;

Super Contributor
Posts: 328

## Re: How to sum element from n and below

Thanks thanks a lot can I do I preserve the order?

Solution
‎01-09-2014 08:25 PM
PROC Star
Posts: 8,164

## Re: How to sum element from n and below

I'm confused!  I thought the code I suggested DID preserve the order in both cases.

Super Contributor
Posts: 328

## Re: How to sum element from n and below

The second code is good, this is the output of the first

 The SAS System

 1 2 3 4 5 4 2 4 6 3 3 9 2 2 11 2 1 13

Super Contributor
Posts: 328