DATA Step, Macro, Functions and more

Sum from bottom to top

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

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
2 weeks ago
Super User
Posts: 6,544

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. 

View solution in original post


All Replies
PROC Star
Posts: 1,357

Re: Sum from bottom to top

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

Contributor
Posts: 24

Re: Sum from bottom to top

Posted in reply to novinosrin

OVar      NVar

1             55

2             54

3             52

4             49

5             45             

6             40

7             34

8             27

9             19

10           10

PROC Star
Posts: 1,357

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=∑
output;
end;
else do;
nvar=nvar-k;
output;
end;
drop k;
run;
Contributor
Posts: 24

Re: Sum from bottom to top

Posted in reply to novinosrin

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

 

PROC Star
Posts: 1,357

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
2 weeks ago
Super User
Posts: 6,544

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: 24

Re: Sum from bottom to top

Posted in reply to Astounding

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. 

 

Enjoy your Friday!

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 117 views
  • 2 likes
  • 3 in conversation