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

Dear SAS community, 

 

I have a very simple calculation problem that I can't quite seem to solve using a combination of do loops, lag functions and a retain statement.  In general, I would like to calculate a new denominator based on values from a previous numerator observation.    For example on Day 2 - the denom = 499 because on Day 1 denom of 500 - numer of 1 = 499.  For day 3, the denom = 497 because the day 2 denom of 499 - day 2 numer of 2 = 497. Below is what I would like my final dataset to look like. Thoughts?

 

Thank you!

 

data want;

input day numer denom;

datalines;;

1 1 500

2 2 499

3 1 497

4 1 496

5 2 495

;;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

It's not clear what you start with. 

Perhaps this will help. 

 

 

 

 

Retain denom2;

 

Y = lag(numer);

if _n_ ne 1 then denom2= denom2 - y;

Else denom2=denom;

View solution in original post

4 REPLIES 4
Reeza
Super User

It's not clear what you start with. 

Perhaps this will help. 

 

 

 

 

Retain denom2;

 

Y = lag(numer);

if _n_ ne 1 then denom2= denom2 - y;

Else denom2=denom;

sophia_SAS
Obsidian | Level 7
This is great! Thank you so much!
Astounding
PROC Star

I would look at it in this way:

 

data want;

set have;

new_denom = 500 - total_numer;

total_numer + numer;

run;

 

Adjustments could be made if you don't want to start at 500 each time.  But the subtraction has to take place first, before recalculating the TOTAL_NUMER.

sophia_SAS
Obsidian | Level 7
This also works great! Thank you both for responding so quickly.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2302 views
  • 0 likes
  • 3 in conversation