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

Hi everyone, 

I have a sasdataset that looks like this. I need to recalculate the total column using values from the previous month and ratio from the current month. I tried using lag and retain function but I couldn’t get the correct answer. Any suggestion?

SAS dataset;

data t1;

input group $ amt ratio month total;

datalines;

A 1200 0.2 1 240

B 3290 0.1 2 0

C 2000 0.1 3 0

A 1000 0.4 1 240

B 5000 0.5 2 0

C 1430 0.6 3 0

;

run;

  

data t2;

set t1;

if month gt 2 then do;

total = ratio * lag(total);

end;

retain total;

run;

 

desire results:

A 1200 0.2 1 240

B 3290 0.1 2 24

C 2000 0.1 3 2.4

A 1000 0.4 1 240

B 5000 0.5 2 120

C 1430 0.6 3 72

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's a way that should work:

 

data t2;

prior_total = total;

set t1;

if month > 1 then total = ratio * prior_total;

run;

 

A couple of notes ... you used gt as the comparison.  But the results are consistent with ge, not gt (hence month > 1 in my code).  Also, there is no need to retain TOTAL.  Since it is part of an incoming SAS data set, it is automatically retained.  It's up to you if you want to drop PRIOR_TOTAL.  You might want to keep it the first time, to better understand how the program works.

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Looking at your desired data output, I'm guessing that this has to happen only for Group B and C and not when the group is equal to A? 

Reeza
Super User

You can't calculate LAG conditionally. 

Calculate lag outside of the IF condition and then you can use it in the IF condition.

Astounding
PROC Star

Here's a way that should work:

 

data t2;

prior_total = total;

set t1;

if month > 1 then total = ratio * prior_total;

run;

 

A couple of notes ... you used gt as the comparison.  But the results are consistent with ge, not gt (hence month > 1 in my code).  Also, there is no need to retain TOTAL.  Since it is part of an incoming SAS data set, it is automatically retained.  It's up to you if you want to drop PRIOR_TOTAL.  You might want to keep it the first time, to better understand how the program works.

thomp7050
Pyrite | Level 9
data t1;
input group $ amt ratio month total;
datalines;
A 1200 0.2 1 240
B 3290 0.1 2 0
C 2000 0.1 3 0
A 1000 0.4 1 240
B 5000 0.5 2 0
C 1430 0.6 3 0
;
run; 

data t2;
set t1_NEW; 
FORMAT NEWTOTAL 8.2;
retain NEWTOTAL; 
IF MONTH = 1 THEN DO;
NEWTOTAL = TOTAL;
END;
if month GE 2 then do;
NEWTOTAL = ratio * NEWTOTAL;
end;
run;

Edit: Sorry, forgot the final run command!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 813 views
  • 0 likes
  • 5 in conversation