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 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 642 views
  • 0 likes
  • 2 in conversation