BookmarkSubscribeRSS Feed
rohithverma
Obsidian | Level 7

I have a data like below : 

 Loanno        dramt       refno     creditamt

     .                  .              1234      1

     .                .                1234      1

    .                  .               1234     100

1234              101               .          .  

I need output like below 

dataset1:-

Loanno         dramt         refno     creditamt

     .                   .               1234       100

     .                  .                1234         1                       

1234               101              .             .           

dataset2:-

Loanno          dramt       refno     creditamt

 .                       .             1234        1

 

Here we can find that sum of dramt 101 is matching by the summation of two CREDITAMT observations .So the observations that makes  matching count will be seperated in one dataset and the other in another dataset should be saved.  Please help me .

Thanks in advance

7 REPLIES 7
rohithverma
Obsidian | Level 7

 

I have a data like below : 

 Loanno        dramt       refno     creditamt

     .                  .              1234      1

     .                .                1234      1

    .                  .               1234     100

1234              101               .          .  

I need output like below 

dataset1:-

Loanno         dramt         refno     creditamt

     .                   .               1234       100

     .                  .                1234         1                       

1234               101              .             .           

dataset2:-

Loanno          dramt       refno     creditamt

 .                       .             1234        1

 

Here we can find that sum of dramt 101 is matching by the summation of two CREDITAMT observations .So the observations that makes  matching count will be seperated in one dataset and the other in another dataset should be saved.  Please help me .

Thanks in advance

PaigeMiller
Diamond | Level 26

We need to have your input data provided as working SAS data step code.

--
Paige Miller
smantha
Lapis Lazuli | Level 10
proc sort data=have; by  Loanno        dramt       refno     creditamt;
run;
data dataset1 dataset2;
set have;
by  Loanno        dramt       refno     creditamt;
if first.creditamt then output dataset1;
else output dataset2;
run;

I hope the above code is what you want.

Reeza
Super User

@rohithverma please do not post the same question multiple times. 

 

You need to expand your sample beyond a single situation. @smantha code works in this exact example but I suspect it won't generalize to your actual data and situation.

 

 

 

 

rohithverma
Obsidian | Level 7
Yes, that's what when the sum of dramt equals to credit amount then those observations should be extracted to one dataset and the rest in other.
smantha
Lapis Lazuli | Level 10

Loanno        dramt       refno     creditamt

     .                  .              1234      1

     .                .                1234      1

    .                  .               1234     100

1234              101               .          .  

I need output like below 

dataset1:-

Loanno         dramt         refno     creditamt

     .                   .               1234       100

     .                  .                1234         1                       

1234               101              .             .           

dataset2:-

Loanno          dramt       refno     creditamt

 .                       .             1234        1

 

Rohith, I am making an assumption that  row 3 is unique and does not have repeated values.


data have1(rename=(loanno=refno)) have2(keep=refno creditamt);

set have;

if missing (loanno) then output have1;

else output have2;

run;

proc sort data=have1 out=have1(keep=refno dramt); by refno;

proc sort data=have2 out=have1(keep=refno creditamt); by refno descending creditamt;



data want1 want2;

merge have1 have2;

by refno;

retain running_total 0;

if first.refno then running_total=creditamt;

else running_total=creditamt + running_total;

if running_total <= then output want1;

else output want2;

run; 

 

 

 

smantha
Lapis Lazuli | Level 10
data have1(rename=(loanno=refno)) have2(keep=refno creditamt);

set have;

if missing (loanno) then output have1;

else output have2;

run;

proc sort data=have1 out=have1(keep=refno dramt); by refno;

proc sort data=have2 out=have1(keep=refno creditamt) nodupkey; by refno descending creditamt;



data want1 want2;

merge have1 have2;

by refno;

retain running_total 0;

if first.refno then running_total=creditamt;

else running_total=creditamt + running_total;

if running_total <= then output want1;

else output want2;

run; 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 7 replies
  • 574 views
  • 3 likes
  • 4 in conversation