amt new_amt
2 2
-3 0
-4 0
5 5(-7)=-2 so should come zero and (-2 remaining value)
6 -2+6=4 (value 4 should come)(until value get positive)
7 7
-4 0
2 -4+2=-2 (negative so value should come 0)
finally
amt new_amt
2 2
-3 0
-4 0
5 0
6 4
7 7
-4 0
2 0
we have one data like negative and positive values are there if negative value is there then we have to subtract next value until get positive. I have give raw data and final data
Do something like this
data have;
input amt;
datalines;
2
-3
-4
5
6
7
-4
2
;
data want(drop = n);
set have;
new_amt = amt;
if new_amt < 0 then do;
n + new_amt;
new_amt = 0;
end;
if new_amt > 0 & n < 0 then do;
new_amt = max(0, new_amt + n);
n = min(n + amt, 0);
end;
run;
Result:
amt new_amt 2 2 -3 0 -4 0 5 0 6 4 7 7 -4 0 2 0
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.