BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Kearthana1
Calcite | Level 5

If the loan count is 1 then merge to the next row. If last observation then merge with the previous row. Greatly appreciate your help in resolving this.

Here is my data:

Original BalanceLoan CountCurr BalanceContrib IO BalanceContrib%WACWAM
(25,000 - 50,000]1116,2947,4120.04.47317
(50,000 - 75,000]12711,04643,8120.24.52337
(75,000 - 100,000]332,906,308186,1900.84.49331
(100,000 - 125,000]242,651,113144,2960.64.48332
(125,000 - 150,000]15,807,462383,1681.74.51331
(150,000 - 175,000]599,304,718643,0802.94.52329
(175,000 - 200,000]8315,377,836984,2864.44.50342
(200,000 - 225,000]6914,498,236889,7854.04.50340
(225,000 - 250,000]8419,678,0151,278,4855.74.50340
(250,000 - 275,000]7619,644,5381,156,3955.24.50339
(275,000 - 300,000]120,128,6571,117,1355.04.50343

 

Want:

Original BalanceLoan CountCurr BalanceContrib IO BalanceContrib%WACWAM
(25,000 -75,000]13827,34051,2240.24.50327
(75,000 - 100,000]332,906,308186,1900.84.49331
(100,000 - 125,000]242,651,113144,2960.64.48332
(125,000 - 175,000]6015,112,1801,026,2482.34.52330
(175,000 - 200,000]8315,377,836984,2864.44.50342
(200,000 - 225,000]6914,498,236889,7854.04.50340
(225,000 - 250,000]8419,678,0151,278,4855.74.50340
(250,000 - 300,000]7739,773,1942,273,5305.14.50341
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

Try this:

data have;
infile cards dlm="|";
input OriginalBalance :$32. LoanCount CurrBalance :comma12. ContribIOBalance :comma12.;

OB2 = scan(OriginalBalance,2,'[( -)]');
format CurrBalance ContribIOBalance dollar12.2;
cards;
(25,000 - 50,000]|1|116,294|7,412|0.0|4.47|317
(50,000 - 75,000]|12|711,046|43,812|0.2|4.52|337
(75,000 - 100,000]|33|2,906,308|186,190|0.8|4.49|331
(100,000 - 125,000]|24|2,651,113|144,296|0.6|4.48|332
(125,000 - 150,000]|1|5,807,462|383,168|1.7|4.51|331
(150,000 - 175,000]|59|9,304,718|643,080|2.9|4.52|329
(175,000 - 200,000]|83|15,377,836|984,286|4.4|4.50|342
(200,000 - 225,000]|69|14,498,236|889,785|4.0|4.50|340
(225,000 - 250,000]|84|19,678,015|1,278,485|5.7|4.50|340
(250,000 - 275,000]|76|19,644,538|1,156,395|5.2|4.50|339
(275,000 - 300,000]|1|20,128,657|1,117,135|5.0|4.50|343
;
run;
proc print;
run;


data have2;
do n=1 by 1 until(LoanCount>1 or _E_);
  set have end=_E_;
  grp++(1=n);
  grp+-(_E_=LoanCount);
  output;
end;
run;
proc print;
run;

data want;

  length OriginalBalance2 $ 32;
  call missing(OriginalBalance2, n, cumB, cumIOB, cumLC);
  do until(last.grp);
    set have2 end=_E_;
    by grp;
    if first.grp then OB1 = scan(OriginalBalance,1,'[( -)]');;
    cumB+CurrBalance;
    cumIOB+ContribIOBalance;
    cumLC+LoanCount;
  end;

  OB2 = scan(OriginalBalance,2,'[( -)]');
  OriginalBalance2 = cats("(",OB1,"-",OB2,"]");

  drop CurrBalance ContribIOBalance LoanCount OriginalBalance OB1 OB2 n grp;
  rename
    cumB=CurrBalance
    cumIOB=ContribIOBalance
    cumLC=LoanCount
    OriginalBalance2=OriginalBalance
  ;
  format cumB cumIOB dollar12.2;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

More information is requested. Please explain how the numbers are calculated when combining rows. Loan count, Curr balance and Contrib IO balance are obvious, the remaining columns it is not clear how you are getting the result.

 

Please provide data as working SAS data step code (instructions and examples). We do not like to work with Excel files or copy/paste from Excel or screen captures.

--
Paige Miller
yabwon
Amethyst | Level 16

Try this, conditional DOW-loop should do the job:

 

data have;
infile cards dlm="|";
input OriginalBalance :$32. LoanCount CurrBalance :comma12. ContribIOBalance :comma12.;

OB2 = scan(OriginalBalance,2,'[( -)]');
format CurrBalance ContribIOBalance dollar12.2;
cards;
(25,000 - 50,000]|1|116,294|7,412|0.0|4.47|317
(50,000 - 75,000]|12|711,046|43,812|0.2|4.52|337
(75,000 - 100,000]|33|2,906,308|186,190|0.8|4.49|331
(100,000 - 125,000]|24|2,651,113|144,296|0.6|4.48|332
(125,000 - 150,000]|1|5,807,462|383,168|1.7|4.51|331
(150,000 - 175,000]|59|9,304,718|643,080|2.9|4.52|329
(175,000 - 200,000]|83|15,377,836|984,286|4.4|4.50|342
(200,000 - 225,000]|69|14,498,236|889,785|4.0|4.50|340
(225,000 - 250,000]|84|19,678,015|1,278,485|5.7|4.50|340
(250,000 - 275,000]|76|19,644,538|1,156,395|5.2|4.50|339
(275,000 - 300,000]|1|20,128,657|1,117,135|5.0|4.50|343
;
run;
proc print;
run;

data want;

  length OriginalBalance2 $ 32;
  call missing(OriginalBalance2, n, cumB, cumIOB, cumLC);
  do n=1 by 1 until(LoanCount>1);
    set have;
    if 1=n then OB1 = scan(OriginalBalance,1,'[( -)]');;
    cumB+CurrBalance;
    cumIOB+ContribIOBalance;
    cumLC+LoanCount;
  end;

  OB2 = scan(OriginalBalance,2,'[( -)]');
  OriginalBalance2 = cats("(",OB1,"-",OB2,"]");

  drop CurrBalance ContribIOBalance LoanCount OriginalBalance OB1 OB2 n;
  rename
    cumB=CurrBalance
    cumIOB=ContribIOBalance
    cumLC=LoanCount
    OriginalBalance2=OriginalBalance
  ;
  format cumB cumIOB dollar12.2;
run;
proc print;
run;

But calculation of: Contrib%, WAC, and WAM you have to do yourself. 

 

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kearthana1
Calcite | Level 5

Thank you so much! The last row with loan count 1 is not getting added to the last but one row and is getting dropped. Can you please advise how to handle it?

yabwon
Amethyst | Level 16

Try this:

data have;
infile cards dlm="|";
input OriginalBalance :$32. LoanCount CurrBalance :comma12. ContribIOBalance :comma12.;

OB2 = scan(OriginalBalance,2,'[( -)]');
format CurrBalance ContribIOBalance dollar12.2;
cards;
(25,000 - 50,000]|1|116,294|7,412|0.0|4.47|317
(50,000 - 75,000]|12|711,046|43,812|0.2|4.52|337
(75,000 - 100,000]|33|2,906,308|186,190|0.8|4.49|331
(100,000 - 125,000]|24|2,651,113|144,296|0.6|4.48|332
(125,000 - 150,000]|1|5,807,462|383,168|1.7|4.51|331
(150,000 - 175,000]|59|9,304,718|643,080|2.9|4.52|329
(175,000 - 200,000]|83|15,377,836|984,286|4.4|4.50|342
(200,000 - 225,000]|69|14,498,236|889,785|4.0|4.50|340
(225,000 - 250,000]|84|19,678,015|1,278,485|5.7|4.50|340
(250,000 - 275,000]|76|19,644,538|1,156,395|5.2|4.50|339
(275,000 - 300,000]|1|20,128,657|1,117,135|5.0|4.50|343
;
run;
proc print;
run;


data have2;
do n=1 by 1 until(LoanCount>1 or _E_);
  set have end=_E_;
  grp++(1=n);
  grp+-(_E_=LoanCount);
  output;
end;
run;
proc print;
run;

data want;

  length OriginalBalance2 $ 32;
  call missing(OriginalBalance2, n, cumB, cumIOB, cumLC);
  do until(last.grp);
    set have2 end=_E_;
    by grp;
    if first.grp then OB1 = scan(OriginalBalance,1,'[( -)]');;
    cumB+CurrBalance;
    cumIOB+ContribIOBalance;
    cumLC+LoanCount;
  end;

  OB2 = scan(OriginalBalance,2,'[( -)]');
  OriginalBalance2 = cats("(",OB1,"-",OB2,"]");

  drop CurrBalance ContribIOBalance LoanCount OriginalBalance OB1 OB2 n grp;
  rename
    cumB=CurrBalance
    cumIOB=ContribIOBalance
    cumLC=LoanCount
    OriginalBalance2=OriginalBalance
  ;
  format cumB cumIOB dollar12.2;
run;
proc print;
run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Kearthana1
Calcite | Level 5

Thank you so much! This works perfectly.

Kearthana1
Calcite | Level 5
If the last two rows are with loan count 1 then I see the error message:
ERROR: BY variables are not properly sorted on data set WORK.HAVE2.
Can you please help?
ballardw
Super User

This looks a lot like a report. If this is a report generated by SAS code you post the code?

 

It might be that the fix might be to update or create a custom format for the values of the original balance column.

 


@Kearthana1 wrote:

If the loan count is 1 then merge to the next row. If last observation then merge with the previous row. Greatly appreciate your help in resolving this.

Here is my data:

Original Balance Loan Count Curr Balance Contrib IO Balance Contrib% WAC WAM
(25,000 - 50,000] 1 116,294 7,412 0.0 4.47 317
(50,000 - 75,000] 12 711,046 43,812 0.2 4.52 337
(75,000 - 100,000] 33 2,906,308 186,190 0.8 4.49 331
(100,000 - 125,000] 24 2,651,113 144,296 0.6 4.48 332
(125,000 - 150,000] 1 5,807,462 383,168 1.7 4.51 331
(150,000 - 175,000] 59 9,304,718 643,080 2.9 4.52 329
(175,000 - 200,000] 83 15,377,836 984,286 4.4 4.50 342
(200,000 - 225,000] 69 14,498,236 889,785 4.0 4.50 340
(225,000 - 250,000] 84 19,678,015 1,278,485 5.7 4.50 340
(250,000 - 275,000] 76 19,644,538 1,156,395 5.2 4.50 339
(275,000 - 300,000] 1 20,128,657 1,117,135 5.0 4.50 343

 

Want:

Original Balance Loan Count Curr Balance Contrib IO Balance Contrib% WAC WAM
(25,000 -75,000] 13 827,340 51,224 0.2 4.50 327
(75,000 - 100,000] 33 2,906,308 186,190 0.8 4.49 331
(100,000 - 125,000] 24 2,651,113 144,296 0.6 4.48 332
(125,000 - 175,000] 60 15,112,180 1,026,248 2.3 4.52 330
(175,000 - 200,000] 83 15,377,836 984,286 4.4 4.50 342
(200,000 - 225,000] 69 14,498,236 889,785 4.0 4.50 340
(225,000 - 250,000] 84 19,678,015 1,278,485 5.7 4.50 340
(250,000 - 300,000] 77 39,773,194 2,273,530 5.1 4.50 341

 

Kearthana1
Calcite | Level 5
Original BalanceLoan CountCurr BalanceContrib IO BalanceContrib%WACWAM
(25,000 - 50,000]1116,2947,4120.04.47317
(50,000 - 75,000]12711,04643,8120.24.52337
(75,000 - 100,000]332,906,308186,1900.84.49331
(100,000 - 125,000]242,651,113144,2960.64.48332
(125,000 - 150,000]15,807,462383,1681.74.51331
(150,000 - 175,000]599,304,718643,0802.94.52329
(175,000 - 200,000]8315,377,836984,2864.44.50342
(200,000 - 225,000]6914,498,236889,7854.04.50340
(225,000 - 250,000]8419,678,0151,278,4855.74.50340
(250,000 - 275,000]119,644,5381,156,3955.24.50339
(275,000 - 300,000]120,128,6571,117,1355.04.50343

 

If the last two rows are 1 then I see grp not sorted properly error. Also is it possible to merge the last two rows with the 225,000 - 250,000 bucket?

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
  • 8 replies
  • 1761 views
  • 0 likes
  • 4 in conversation