My first post... please be kind 😊 I am using SAS Studio. I have a base table of date, time, and coin ticker, as follows: data BaseTXs;
infile datalines delimiter=',';
input
date :yymmdd10.
time :time.
coin $;
format
date yymmdd10.
time timeampm.;
datalines;
2020-09-01,9:29:55 PM,BTC
2020-09-01,9:58:00 PM,BTC
2020-09-08,10:30:42 AM,BTC
2020-09-09,4:20:00 PM,BTC
; To my base table, I simply want to append the price of bitcoin using my PriceLookup table, picking the price that is closest to my date and time. Here is my example PriceLookup table: data PriceLookup;
infile datalines delimiter=',' truncover;
input
price
date :yymmdd10.
time :time.
coin $;
format
price 8.2
date yymmdd10.
time timeampm.;
datalines;
351.05,2020-08-31,11:59:59 PM,BTC
400.45,2020-09-01,8:16:41 AM,BTC
425.02,2020-09-01,9:18:45 PM,BTC
378.05,2020-09-01,10:13:47 PM,BTC
755.66,2020-09-07,6:23:47 PM,BTC
789.99,2020-09-08,9:25:00 AM,BTC
804.33,2020-09-08,1:55:54 PM,BTC
1013.82,2020-09-09,3:05:00 AM,BTC
1020.10,2020-09-09,6:00:00 PM,BTC
1015.05,2020-09-10,8:00:00 PM,BTC
999.65,2020-09-10,8:15:05 PM,BTC
; So my final output table should look like this: data BaseTXs_fin;
infile datalines delimiter=',';
input
date :yymmdd10.
time :time.
coin $
price;
format
date yymmdd10.
time timeampm.
price 8.2;
datalines;
2020-09-01,9:29:55 PM,BTC,425.02
2020-09-01,9:58:00 PM,BTC,378.05
2020-09-08,10:30:42 AM,BTC,789.99
2020-09-09,4:20:00 PM,BTC,1020.10
; I have seen several posts/articles showing how this can be done in one PROC SQL step, but I can't seem to alter the code to fit my needs (I am mostly unsure how to setup the WHERE, GROUP BY, and HAVING statements). Here, for example: https://www.lexjansen.com/pharmasug/2003/CodersCorner/cc001.pdf Any help would be much appreciated! (My request is actually a bit simplified, as my production base table consists of more coins than just bitcoin, and each different coin has its own separate PriceLookup table. So I'd need to do a merge for each coin. I think I can do this on my own if I can just get the simplified version to work, but if you're up for providing the extra help, I certainly won't complain!)
... View more