I have worked in sas for a little over a year now off and on so I would still consider myself fairly new to the language. I am trying to compute Euclids Method to find the gcd using a do while loop in sas. I have never enjoyed loops, but I did take a stab at the loop and I pasted my code below. I do know that Euclids Method goes as follows:
1. a = b, then a (or b)
2. a>b, then gcd(a-b, b)
3. a<b, then gcd(a, b-a)
I am trying to test the code on two random values like 18 and 48 for example. I want to be able to change a and b to whatever numbers I would like. My starting code is below. Any help would be greatly appreciated. Thank you in advance.
proc iml;
start func(a, b); /*start function*/
do while(b != 0);
t = b;
b = mod(a,b);
a = t;
output;
end;
return(?); /*? unsure what to put here*/
finish(func);
run;
quit;
Since you posted SAS/IML code, I will answer your question by using SAS/IML code.
Your code has a few problems:
1. In SAS, the "not equals" operators is "^=". You were using the C and Java syntax "!=".
2. The return value should be a.
3. In SAS/IML, arguments to a function are passed by reference. If you change an argument in a module, it changes the value that you passed in. For the GCD algorithm this will corrupt the input data, so you should copy the arguments before you begin to modify them.
proc iml;
start EuclidAlg(_a, _b); /*start function*/
a = _a; b = _b;
do while(b ^= 0);
t = b;
b = mod(a,b);
a = t;
end;
return(a);
finish EuclidAlg;
a = 1071;
b = 462;
gcd = EuclidAlg(a, b);
print gcd " is the GCD of " a " and " b;
/* validate by using the built-in GCD function */
validate = gcd(a,b);
print validate;
Is there a particular reason you're making your own function here, instead of using the built in GCD function?
Returns the greatest common divisor for one or more integers.
@alilacey0 wrote:
I have worked in sas for a little over a year now off and on so I would still consider myself fairly new to the language. I am trying to compute Euclids Method to find the gcd using a do while loop in sas. I have never enjoyed loops, but I did take a stab at the loop and I pasted my code below. I do know that Euclids Method goes as follows:
1. a = b, then a (or b)
2. a>b, then gcd(a-b, b)
3. a<b, then gcd(a, b-a)
I am trying to test the code on two random values like 18 and 48 for example. I want to be able to change a and b to whatever numbers I would like. My starting code is below. Any help would be greatly appreciated. Thank you in advance.
proc iml;
start func(a, b); /*start function*/
do while(b != 0);
t = b;
b = mod(a,b);
a = t;
output;
end;
return(?); /*? unsure what to put here*/
finish(func);
run;
quit;
I am trying to familiarize myself with a do while loop and I am able to check my work with the gcd function. So I would like to create my own function with the do while loop inside of a function, almost like "for practice" so I am able to expand on loops, but I am aware of the gcd function.
Defining the function in proc fcmp allows you to use it almost everywhere :
proc fcmp outlib=work.fcmp.util;
function myGcd(x, y);
a = x; b = y;
do while (b ne 0);
t = b;
b = mod(a, b);
a = t;
end;
return (a);
endsub;
run;
options cmplib=(work.fcmp);
data have;
input a b;
datalines;
4 6
1 25
0 12
7 43
10 10
2.4 10
;
data _null_;
set have;
GCD = myGcd(a, b);
check = gcd(a, b);
put (a b gcd check) (=);
run;
proc sql;
select a, b, myGcd(a, b) as GCD
from have;
quit;
I remembered @Rick_SAS has already written a blog about GCD() for SAT Score .
Since you posted SAS/IML code, I will answer your question by using SAS/IML code.
Your code has a few problems:
1. In SAS, the "not equals" operators is "^=". You were using the C and Java syntax "!=".
2. The return value should be a.
3. In SAS/IML, arguments to a function are passed by reference. If you change an argument in a module, it changes the value that you passed in. For the GCD algorithm this will corrupt the input data, so you should copy the arguments before you begin to modify them.
proc iml;
start EuclidAlg(_a, _b); /*start function*/
a = _a; b = _b;
do while(b ^= 0);
t = b;
b = mod(a,b);
a = t;
end;
return(a);
finish EuclidAlg;
a = 1071;
b = 462;
gcd = EuclidAlg(a, b);
print gcd " is the GCD of " a " and " b;
/* validate by using the built-in GCD function */
validate = gcd(a,b);
print validate;
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.