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
;;
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;
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.