Calculating Factorial using SAS

0


In this post, I will show you how to calculate the factorial of a given number using SAS Language.

Explain: For example, the factorial of 5 is equal to 5 x 4 x 3 x 2 x 1 = 120.


Using SAS Macro:

%macro factorial(num);

   %local i result;

   %let result = 1;

   %do i = 1 %to #

      %let result = %eval(&result * &i);

   %end;

   %put Factorial of &num: &result;

%mend;


SAS itself provides a function that is fact to calculate the factorial of a number.

 

Using SAS fact function:

 

data _null_;

x = fact(5);

put "factorial is " x;

run;



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !