BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

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

1 REPLY 1
PeterClemmensen
Tourmaline | Level 20

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 

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 718 views
  • 0 likes
  • 2 in conversation