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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 1620 views
  • 0 likes
  • 3 in conversation