Discussion:
what is sas module operator?
(too old to reply)
Jianping Zhu
2005-05-19 20:23:33 UTC
Permalink
I need to find if a number is odd or ever, what is module operator in sas
Thanks
toby dunn
2005-05-19 20:30:16 UTC
Permalink
Jainping,

data one;
input x ;
cards;
1
2
3
4
5
6
;
run ;

data two ;
set one ;
length y $4. ;

if mod(x,2) then y = 'ODD' ;
else y = 'EVEN' ;

put x= y= ;
run ;




Toby Dunn




From: Jianping Zhu <***@GMAIL.COM>
Reply-To: Jianping Zhu <***@gmail.com>
To: SAS-***@LISTSERV.UGA.EDU
Subject: what is sas module operator?
Date: Thu, 19 May 2005 16:23:33 -0400

I need to find if a number is odd or ever, what is module operator in sas
Thanks
David L. Cassell
2005-05-19 21:04:24 UTC
Permalink
Post by Jianping Zhu
I need to find if a number is odd or ever, what is module operator in
sas

It's not the 'module' operator, it's the 'modulus' function,
from number theory. Try:

y = mod(x,2);

mod(x,2) will take a number X, and subtract multiples of two until the
result is between zero and two (not including two). If you start with
a whole number X, mod(x,2) will give you 1 when your number is odd and
0 when your number is even.

As of SAS 9, mod() has an extra feature, in that it does a certain
amount
of fuzzing too. This is quite handy when you're using mod() on really
large integers or numbers which are not integers. If you are running
SAS 9 and you want the non-fuzzing version, use the MODZ() function
instead.

HTH,
David
--
David Cassell, CSC
***@epa.gov
Senior computing specialist
mathematical statistician
Choate,
2005-05-19 21:40:37 UTC
Permalink
That's cool David -

Modz seems like a good way to get into trouble, you better understand
numeric representation pretty darn good.

If things get too large or small then they both return missing: "Both the
MOD and MODZ functions return a missing value if the remainder cannot be
computed to a precision of approximately three digits or more."

Thanks for the tip, I hadn't thought of using MOD on non-integers. Looks
like it's useful for all rationals.

regards

Paul Choate
DDS Data Extraction
(916) 654-2160

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of David
L. Cassell
Sent: Thursday, May 19, 2005 2:04 PM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: what is sas module operator?
Post by Jianping Zhu
I need to find if a number is odd or ever, what is module operator in
sas

It's not the 'module' operator, it's the 'modulus' function,
from number theory. Try:

y = mod(x,2);

mod(x,2) will take a number X, and subtract multiples of two until the
result is between zero and two (not including two). If you start with
a whole number X, mod(x,2) will give you 1 when your number is odd and
0 when your number is even.

As of SAS 9, mod() has an extra feature, in that it does a certain
amount
of fuzzing too. This is quite handy when you're using mod() on really
large integers or numbers which are not integers. If you are running
SAS 9 and you want the non-fuzzing version, use the MODZ() function
instead.

HTH,
David
--
David Cassell, CSC
***@epa.gov
Senior computing specialist
mathematical statistician
Sigurd Hermansen
2005-05-19 22:21:15 UTC
Permalink
Jianping:
I believe that you are looking for the SAS MOD() [for modulus n,m]
function. If MOD(<number>,2) then odd else even. For example, MOD(11,2)
returns a value of 1 and SAS interprets that as a logical true value,
while MOD(10,2) returns a value of 0 and SAS interprets that as false.
MOD() yields the remainder of an integer divided by smaller integer.
Sig

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
Jianping Zhu
Sent: Thursday, May 19, 2005 4:24 PM
To: SAS-***@LISTSERV.UGA.EDU
Subject: what is sas module operator?


I need to find if a number is odd or ever, what is module operator in
sas Thanks
David L. Cassell
2005-05-20 00:51:51 UTC
Permalink
Post by Choate,
Modz seems like a good way to get into trouble, you better understand
numeric representation pretty darn good.
Actually, it's the way MOD() worked in SAS 8 and earlier versions.
So it's always been a potential problem.
Post by Choate,
If things get too large or small then they both return missing: "Both
the
Post by Choate,
MOD and MODZ functions return a missing value if the remainder cannot
be
Post by Choate,
computed to a precision of approximately three digits or more."
Which is a good thing, in my opinion.
Post by Choate,
Thanks for the tip, I hadn't thought of using MOD on non-integers.
Looks
Post by Choate,
like it's useful for all rationals.
Especially around all of us irrational types. :-)

Actually, you can use MOD() in a lot of arcane places. Try out
this code:


data temp1;
x = 1 + constant('PI');
y = 3 * constant('PI');
z = mod(y,x);
put x= y= z= ;

x = constant('PI');
y = 3 * constant('PI');
z = mod(y,x);
put x= y= z= ;

run;


Enjoy!
David
--
David Cassell, CSC
***@epa.gov
Senior computing specialist
mathematical statistician

Loading...