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.
This formula could be part of a DO loop in a data step. You could also program this in loop in PROC IML.
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).
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;
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.
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
sum
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.