'Sum function in SAS

I am trying to create a small function to do the follow: c = (a/3)*(1/((1+x)*2)+1/((1+x)*4)+1/((1+x)*8))+1/((1+x)*8)

Input variables are a and x. As you can see, the number to multiply by is a multiple of 2 (up to 8).

My main difficult is in the recursive sum. I know that SAS has a SUM function, but I am wondering how to use it in this exercise. I thought the parameter could be (1/((1+x)*n*2) (where n is a number).
Help is welcome. Thanks.

sas


Solution 1:[1]

You can code a loop to iterate the pieces to accumulate. Something like:

data want;
  a = 2;
  x = 7;
  steps = 4;
  S = 0;
  do index = 1 to steps;
    S = sum (S, 1/(1+x)*index*2);
  end;
  c = a/3 * S;
run;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1