BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmj636
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
novinosrin
Tourmaline | Level 20

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

kmj636
Obsidian | Level 7

OVar      NVar

1             55

2             54

3             52

4             49

5             45             

6             40

7             34

8             27

9             19

10           10

novinosrin
Tourmaline | Level 20
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;
kmj636
Obsidian | Level 7

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

 

novinosrin
Tourmaline | Level 20

@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 

Astounding
PROC Star

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. 

kmj636
Obsidian | Level 7

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!

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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