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

I have a dataset with the list of givers and takers present in one column and the amount that is given (the negative values) Or taken (the positive value) by them in another column.

The amount deviation is such that its total sum is equals to 0.

I want to create a dataset consisting of from, amount, to as variables.

The selection of the members should be in sequence for from and to.

Input dataset :
Member Deviation
Aus -2000
Br 3000
Nz -10000
In 4000
No 5000
Br  -3000.36
Nz 2000.36
In -4000.53
No 5000.53


Output dataset:
From   Amount   To
Aus     2000       Br
Nz      1000        Br
Nz      4000        In
Nz      5000        No

Br       2000.36   Nz

Br       1000        No

In        4000.53  No

 

Can anyone help solving it using retain and loops?

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

That was a fun challenge! Here is a solution that works, at least for the data given:

data have;
  length member $3;
  input Member Deviation;
cards;
Aus -2000
Br 3000
Nz -10000
In 4000
No 5000
Br  -3000.36
Nz 2000.36
In -4000.53
No 5000.53
;run;

data want;
  set have(where=(Deviation<0) rename=(Member=From));
  retain Remainder 0;
  if Remainder>0 then do;
    Amount=Min(Remainder,-Deviation);
    output;
    end;
  Remainder=Remainder+Deviation;
  do while(not done and Remainder<0);
    set have(where=(Deviation>0) rename=(Member=To)) end=done;
    Amount=min(-Remainder,Deviation);
    output;
    Remainder=Remainder+Deviation;
    end;
  Keep From Amount To;
run;
    
    

View solution in original post

1 REPLY 1
s_lassen
Meteorite | Level 14

That was a fun challenge! Here is a solution that works, at least for the data given:

data have;
  length member $3;
  input Member Deviation;
cards;
Aus -2000
Br 3000
Nz -10000
In 4000
No 5000
Br  -3000.36
Nz 2000.36
In -4000.53
No 5000.53
;run;

data want;
  set have(where=(Deviation<0) rename=(Member=From));
  retain Remainder 0;
  if Remainder>0 then do;
    Amount=Min(Remainder,-Deviation);
    output;
    end;
  Remainder=Remainder+Deviation;
  do while(not done and Remainder<0);
    set have(where=(Deviation>0) rename=(Member=To)) end=done;
    Amount=min(-Remainder,Deviation);
    output;
    Remainder=Remainder+Deviation;
    end;
  Keep From Amount To;
run;
    
    

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
  • 1 reply
  • 315 views
  • 2 likes
  • 2 in conversation