DATA Step, Macro, Functions and more

Sigma Summation in sas

Reply
New Contributor
Posts: 3

Sigma Summation in sas

How do we go about inserting mathematical functions like this into sas? Sum(i in 1 to n-2) (* Floor (n/) where n is just the natural numbers.

I just want to output two columns, one for the natural numbers and one for out come of the summation above.


Trusted Advisor
Posts: 1,933

Re: Sigma Summation in sas

This formula could be part of a DO loop in a data step. You could also program this in loop in PROC IML.

New Contributor
Posts: 3

Re: Sigma Summation in sas

Posted in reply to PaigeMiller
data some_numbers;

n=0;

a=0;

b=0;

do while(n<1000);

   a= (here is my problem, I want a to be Sum(i in 1 to n-2) (* Floor (n/))

output;

n=n+1;

end;

run;

my over all aim is just to have two columns, one with just n=0 to 1000 or so, and the second column is (a) as above. however my above code wouldn't accept this difficult  for (a).

Super User
Posts: 11,343

Re: Sigma Summation in sas

Provide what you are expecting for the first 3 or 4 outputs (at least). I suspect you may not be expressing your problem quite concisely.

You can factor out the * bit as a constant times each member of series: * (first term) + *(second term + ... +*(nth term) = * sum of terms. So * sum of Floor(n/).

HOWEVER: You have issues with  the value of is going to hit 0 in three iterations as n-2 will be -2 , -1 and 0 and hence undefined.

Maybe you want to start with n=3?

I think you are looking, in a more general sense for :

do while(n<1000);

     a=0;

     do i =1 to (n-2);

        a= a+ (i* Floor (n/i));

     end;

output;

n=n+1;

end;

Super User
Posts: 19,870

Re: Sigma Summation in sas

It sounds like you're treating your data like a matrix and may want to look at SAS/IML.

BASE/SAS programming doesn't quite work the same way, so you need to change the way you think a bit.  It process each line and then moves onto the next, with exceptions that can be programmed in. This method means it doesn't need to read all of the data into memory which is why it handles big data with the same code as small data in a straightforward manner.

At any rate, you need to go back to basic looping structures then when you want to do summations.  Post an example of the data you have and the output you expect. If you're looking to use SAS/IML which is closer to R then mention that as well.

SAS Super FREQ
Posts: 3,755

Re: Sigma Summation in sas

The data step is easy:

data series;
do n = 3 to 10;
   sum = 0;
   do i = 1 to n-2;
      sum = sum + i/ floor(n/i);
   end;
   output;
end;
keep n sum;
run;

proc print;run;

In IML, you can loop over the values of n:

proc iml;

n = T(3:10);

sum = j(nrow(n),1);

do k = 1 to nrow(n);

   i = 1Smiley Sadn-2);

   sum = sum( i / floor( n / i ) );

end;

print n sum;

If you want to get fancy, you can also do compute the summations for all values of n without any loops by using matrices.

SAS Super FREQ
Posts: 3,755

Re: Sigma Summation in sas

Ask a Question
Discussion stats
  • 6 replies
  • 456 views
  • 0 likes
  • 5 in conversation