Solved
Contributor
Posts: 26

# Sum from bottom to top

Hi,

I am working with a SAS dataset and am creating new variables based on previous VARs. I would like to create a new VAR which holds the sums of another column based on the following: 10 obs,

#10 of the NewVar=#10 of OldVar

#9 of NVar= #9+#10 OVar

#8 NVar=#8+#9+#10 OVar ....

and so on.

So the total sum of the column will actually be Obs 1 of the New Var. Does anyone know how to sum backwards. I have been trying to wrap my head around this without changing the sort of the set or exporting and I would like to be able to do this by calling Vars rather than using the actual numbers to calculate, so I can use this in the future quickly.

Thanks for any help and suggestions you might have!

Accepted Solutions
Solution
‎05-11-2018 12:47 PM
Super User
Posts: 6,934

## Re: Sum from bottom to top

Here's an approach you can use.  Depending on the details of what you want, you should be able to tweak it as needed.

First get the total for the next 10 observations.  Then cycle through the same observations and compute:

data want;

newvar = 0;

do k=1 to 10;

set have;

newvar + oldvar;

end;

do k=1 to 10;

set have;

output;

newvar = newvar - oldvar;

end;

run;

Since both SET statements operate independently of one another, both loops read exactly the same observations.

All Replies
Super User
Posts: 2,061

## Re: Sum from bottom to top

Please provide a sample data of what you have and what you want . Thank you

Contributor
Posts: 26

## Re: Sum from bottom to top

OVar      NVar

1             55

2             54

3             52

4             49

5             45

6             40

7             34

8             27

9             19

10           10

Super User
Posts: 2,061

## Re: Sum from bottom to top

``````data have;
input OVar      NVar;
drop nvar;
cards;
1             55
2             54
3             52
4             49
5             45
6             40
7             34
8             27
9             19
10           10
;

proc sql;
select sum(ovar) into :sum
from have;
quit;

data want;
set have;
retain nvar;
k=lag(ovar);
if _n_=1 then do;
nvar=&sum;
output;
end;
else do;
nvar=nvar-k;
output;
end;
drop k;
run;``````
Contributor
Posts: 26

## Re: Sum from bottom to top

Thank you so much!

Just so everyone knows this worked a little better for me in my program then the post I accepted as the answer. The other one I could definitely tweak and works but this one was spot on.

Thanks again - the SAS community people are the best

Super User
Posts: 2,061

## Re: Sum from bottom to top

[ Edited ]

@kmj636  You wrote -"Thanks again - the SAS community people are the best"

We the community(you included) are a team we are the best. It's fun to be here. I like the slogan "ask, find and share" by our managers @ChrisHemedinger et al. I am glad to be so active here that indirectly made my fellow class mates at my university use SAS

Solution
‎05-11-2018 12:47 PM
Super User
Posts: 6,934

## Re: Sum from bottom to top

Here's an approach you can use.  Depending on the details of what you want, you should be able to tweak it as needed.

First get the total for the next 10 observations.  Then cycle through the same observations and compute:

data want;

newvar = 0;

do k=1 to 10;

set have;

newvar + oldvar;

end;

do k=1 to 10;

set have;

output;

newvar = newvar - oldvar;

end;

run;

Since both SET statements operate independently of one another, both loops read exactly the same observations.

Contributor
Posts: 26

## Re: Sum from bottom to top

You are awesome! Yes, I think I can get this to do what I want.

Thank you so much for everyone's help and time.

☑ This topic is solved.