BookmarkSubscribeRSS Feed
Tim123
Calcite | Level 5

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.


6 REPLIES 6
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
Tim123
Calcite | Level 5
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).

ballardw
Super User

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;

Reeza
Super User

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.

Rick_SAS
SAS Super FREQ

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 = 1:(n-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-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1908 views
  • 0 likes
  • 5 in conversation