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.
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.
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;
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
Then mark his/her answer as being correct!
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;
1 | 0:1 | function odd_slk( x ) label= "Excel ODD"; |
2 | 5:1 | e = INT(x) + SIGN(MOD(x, INT(x))); |
3 | 6:1 | if ( mod(e,2) eq 0 ) then |
4 | 7:1 | e = e += SIGN(x); |
5 | 8:1 | _odd_slk_ = e; |
6 | 9:1 | endsub; |
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.
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.
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.