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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.