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

Astounding,

In all fairness, FriedEgg's and DataNull's proposed solutions also appear to work perfectly.  However, I *really* like your one liner.

In case I've never mentioned it here, I'm a psychologist/director, not a programmer.  The role I think I play here, and used to do on SAS-L, is to help refine interesting looking questions and, at the same time, motivate others to show the rest of us how it should/can be done.

Astounding
PROC Star

There's no ODD function that I know of, but this approach may work.

lower_odd = floor(original_number);

if mod(lower_odd,2) = 0 then lower_odd = lower_odd - 1;

if original_number - lower_odd < 1 then closest_odd = lower_odd;

else closest_odd = lower_odd + 2;

I use base SAS, hardly ever in EG.  So I don't know if you'll need to adapt this or not.  And you'll have to set up rules for ties.  What if the original number is 4?  What if it is -4?  All of that can be coded though.

Good luck.

FriedEgg
SAS Employee

This matches the output from Excel for the odd function using all Art's examples:

procfcmp;

function excel_odd(n);

  return(ifn(n ge 0,ceil(n)+(mod(ceil(n),2)=0),floor(n)-(mod(floor(n),2)=0)));

endsub;

array x[31](-9 -8-7 -6-5 -4.00001-4 -3-2 -1.5-1 -.5-.25 -.1-.00001 0.00001 .1.25 .51 1.52 34 4.000015 67 89);

do i=1to dim(x);

  excel_odd=excel_odd(x);

  put excel_odd=;

end;

run;


PJorge
Calcite | Level 5

Just want to let you know that I got the results I wanted. All of your input were amazing and really helped me.

Thank you all

PS- I used the one line formula suggested by Astounding

art297
Opal | Level 21

Then mark his/her answer as being correct!

FriedEgg
SAS Employee

I just remembered something on this old topic.  SAS already has functions for many Excel functions, they just aren't enabled by default.  I beleive they were developed for part of SAS/Access for PC Files, so if you license that product you should have the following function data catalog in your sashelp library: SLKWXL.  You can see all the contents of this file by running the following code:

proc fcmp inlib=sashelp.slkwxl listall;

run;

Inside this file there is a function odd_slk(), this function implements the ODD function in Excel as follows:

proc fcmp inlib=sashelp.slkwxl;

listfunc odd_slk;

run;

     

10:1function odd_slk( x ) label= "Excel ODD";
25:1e = INT(x) + SIGN(MOD(x, INT(x)));
36:1if ( mod(e,2) eq 0 ) then
47:1e = e += SIGN(x);
58:1_odd_slk_ = e;
69:1endsub;

options cmplib=sashelp.slkwxl;

data foo;

input num @@;

odd=odd_slk(num);

cards;

-9 -8 -7 -6 -5 -4.00001

-4 -3 -2 -1.5 -1 -.5

-.25 -.1 -.00001 0.00001

.1 .25 .51 1.52 34 4.000015

67 89

;

run;

It doesn't work very well, unfortunately, since it will error when INT(num) = 0, but I assume someone may find the other excel functions in this file of benefit or at least directional to develop your own, working, function if others have this same error problem.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 20 replies
  • 7285 views
  • 1 like
  • 6 in conversation