SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
J_Yetman
Fluorite | Level 6

I need to calculate the IRR function with a large number of payments (loans with 360 monthly payments).

 

The IRR function does calculate the correct value if I list out all 360 payments one by one.  I'm looking for a better way to write the code so I'm not writing out the monthly payment 360 times in the code.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@J_Yetman wrote:

Thanks, I understand now.  They currently aren't in a data set (at least not in the way that you mean - the value of the payment is pulled from a single variable field in a table).  So I'd have to create one.


You could just use a little code generation to reuse the same variable name over and over.

For example this code uses the value of the variable PAYMENT 5 times (the REPEAT function wants the number of EXTRA copies not the total number of copies).

data test;
  input id payment ;
  irr5=IRR(1,-100000,of %sysfunc(repeat(%str(payment ),4)));
cards;
1 100
2 200
3 300
4 10000
;

 

View solution in original post

5 REPLIES 5
ballardw
Super User

Do you have the payments in a data set? If so, show an example of the data set.

 

Here is one example using an array to hold multiple payments for use in IRR function. For pretty obvious reasons I'm not going to dummy up 100's of values.

data have;
   input p1-p5;
datalines;
-100 200 100 200 100
-50   10  20  30  40
;

data want;
  set have;
  array p(*) p1-p5;
  rate=irr(1,of p(*));
run;

@J_Yetman wrote:

I need to calculate the IRR function with a large number of payments (loans with 360 monthly payments).

 

The IRR function does calculate the correct value if I list out all 360 payments one by one.  I'm looking for a better way to write the code so I'm not writing out the monthly payment 360 times in the code.


 

J_Yetman
Fluorite | Level 6

The payments are not variable.  It's the same payment repeating.  So it would be something like:

 

IRR(1,-100000,500,500,500,....,500)

 

 

ballardw
Super User

Didn't answer the question: WHERE do these values currently reside?

If they are not in a data set then you basically have no option but to to type them in. Or read them into a data set to allow something similar to the way I showed above.

If they are in a data set, then show the structure of that data set.

 

IF it is ALWAYS the same value then you could build that in a data step like so;

data have;
  array p (50) ;
  p1=-10000;
  do i=2 to 50;
    p[i]=500;
  end;
  rate = irr(1,of p(*));
run;
J_Yetman
Fluorite | Level 6

Thanks, I understand now.  They currently aren't in a data set (at least not in the way that you mean - the value of the payment is pulled from a single variable field in a table).  So I'd have to create one.

Tom
Super User Tom
Super User

@J_Yetman wrote:

Thanks, I understand now.  They currently aren't in a data set (at least not in the way that you mean - the value of the payment is pulled from a single variable field in a table).  So I'd have to create one.


You could just use a little code generation to reuse the same variable name over and over.

For example this code uses the value of the variable PAYMENT 5 times (the REPEAT function wants the number of EXTRA copies not the total number of copies).

data test;
  input id payment ;
  irr5=IRR(1,-100000,of %sysfunc(repeat(%str(payment ),4)));
cards;
1 100
2 200
3 300
4 10000
;

 

sas-innovate-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 5 replies
  • 1366 views
  • 1 like
  • 3 in conversation