BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
alilacey0
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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;

View solution in original post

5 REPLIES 5
Reeza
Super User

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.

http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p0413szaijg3afn1x426zwmihusz.htm&...

 

 


@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;


 

alilacey0
Fluorite | Level 6

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.

PGStats
Opal | Level 21

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;
PG
Ksharp
Super User

I remembered @Rick_SAS has already written a blog about GCD() for SAT Score .

Rick_SAS
SAS Super FREQ

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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