Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

look around

Reply
Contributor
Posts: 29

look around

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

Valued Guide
Posts: 505

Re: look around

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;


Contributor
Posts: 29

Re: look around

Posted in reply to rogerjdeangelis

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. 

 

 

Ask a Question
Discussion stats
  • 2 replies
  • 166 views
  • 1 like
  • 2 in conversation