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

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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