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

Hi,

I have a fairly large data set (around 1 million observations) that looks something like the following table:

ID         n         P          y

1          5         11.4      0.04

2          8         7.6        0.02

3          13       12.2       0.01

4          4         3.9         0.07

...

I want to create a column D that calculates the following for each row: P * [ (1+y)^0 + (1+y)^1 + (1+y)^2 + ... + (1+y)^n ]

I tried using a for loop but wasn't able to perform the required calculation. Any help would be much appreciated.

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Seems like a simple problem.  If N gets too big you might have problems with numeric overflow.

data have;

  input ID n P y @@;

cards;

1 5 11.4 0.04 2 8 7.6 0.02 3 13 12.2 0.01 4 4 3.9 0.07

run;

data want ;

  set have ;

  d = 0;

  do i=0 to n ;

    d=d+(1+y)**i;

  end;

  d = p*d;

  drop i ;

run;

Obs    ID     n      P       y        d

1      1     5    11.4    0.04     75.616

2      2     8     7.6    0.02     74.135

3      3    13    12.2    0.01    182.359

4      4     4     3.9    0.07     22.428

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Seems like a simple problem.  If N gets too big you might have problems with numeric overflow.

data have;

  input ID n P y @@;

cards;

1 5 11.4 0.04 2 8 7.6 0.02 3 13 12.2 0.01 4 4 3.9 0.07

run;

data want ;

  set have ;

  d = 0;

  do i=0 to n ;

    d=d+(1+y)**i;

  end;

  d = p*d;

  drop i ;

run;

Obs    ID     n      P       y        d

1      1     5    11.4    0.04     75.616

2      2     8     7.6    0.02     74.135

3      3    13    12.2    0.01    182.359

4      4     4     3.9    0.07     22.428

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