BookmarkSubscribeRSS Feed
Xinhui
Obsidian | Level 7

How  to pick up the nearest number of 26.33 from data set(25,25.5,26,26.5,27).

2 REPLIES 2
rogerjdeangelis
Barite | Level 11
How  to pick up the nearest number of 26.33 from data set(25,25.5,26,26.5,27).

If you have the IML interface to R you should
be able to just cut and paste into the interface.

inspired by
https://goo.gl/Wc5cGb
https://communities.sas.com/t5/SAS-Data-Mining/look-around/m-p/336290


HAVE

    25,  25.5,  26,  26.5,  27

  and

    26,33

WANT

    26.5


FULL SOLUTION
=============

  R    nums<-c(25,25.5,26,26.5,27);
       nums[which.min(abs(nums - 26.33))];

 Sas   array nums[2,5] _temporary_ (25,25.5,26,26.5,27 5*26.33);
       dif=nums[1,i]-nums[2,i];
       if abs(dif) < big then do; big=abs(dif); sav=nums[1,i]; end;

FULL SOLUTIONS
==============

*____
|  _ \
| |_) |
|  _ <
|_| \_\
;

%utl_submit_r64('
  nums<-c(25,25.5,26,26.5,27);
  nums[which.min(abs(nums - 26.33))];
');

> nums<-c(25,25.5,26,26.5,27);  nums[which.min(abs(nums - 26.33))];
[1] 26.5
>


*____    _    ____     __        ______  ____
/ ___|  / \  / ___|    \ \      / /  _ \/ ___|
\___ \ / _ \ \___ \ ____\ \ /\ / /| |_) \___ \
 ___) / ___ \ ___) |_____\ V  V / |  __/ ___) |
|____/_/   \_\____/       \_/\_/  |_|   |____/

;

data _null_;

  array nums[2,5] _temporary_ (25,25.5,26,26.5,27 5*26.33);
  big=constant('big');
  do i=1 to 5;
     dif=nums[1,i]-nums[2,i];
     if abs(dif) < big then do; big=abs(dif); sav=nums[1,i]; end;
  end;
  put nums[2,1] sav big;

run;quit;

759   data _null_;
760     array nums[2,5] _temporary_ (25,25.5,26,26.5,27 5*26.33);
761     big=constant('big');
762     do i=1 to 5;
763        dif=nums[1,i]-nums[2,i];
764        if abs(dif) < big then do; big=abs(dif); sav=nums[1,i]; end;
765     end;
766     put nums[2,1] sav big;
767   run;

26.33 26.5 0.17

NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      user cpu time       0.01 seconds
      system cpu time     0.00 seconds
      memory              261.43k
      OS Memory           13540.00k
      Timestamp           02/27/2017 11:51:09 AM
      Step Count                        153  Switch Count  0

767 !     quit;


Xinhui
Obsidian | Level 7

First of all, thank you for your help. The code you gave me works well. There is another problem, however. In my case, we have three variables - the underlying price, the strike price, and the datadate. When the program picks up the strike price that is nearest to the underlying price, I also need it to consider the datadate. Please see the attached document for a sample. 

 

So basically, I need it to pick the nearest strike price within that given date.

 

For example, on 2011/1/16, the underlying price is 26.33. The closest strike price is 26.5. 

On 2011/1/17, the underlying price is 26.11 and the closest strike price is 26. 

 

 

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 choose a machine learning algorithm

Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 725 views
  • 1 like
  • 2 in conversation