- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Is there a particular reason you're making your own function here, instead of using the built in GCD function?
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I remembered @Rick_SAS has already written a blog about GCD() for SAT Score .
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;