I'm good with sql, but it's not the right tool for this job. I have to connect execution quantities for stocks with their last known prices. (That is, I can partially join the tables on date and ticker, but I have to find the price from Prices that is the latest time before the Event's time.) Below is some sample code and a sql statement that works, but is too inefficent my large datasets. I'm wondering if there is some tricky data step with merging that can do this better. data Events; format TradeDate mmddyys10. TradeTime time.; input TradeDate :anydtdte. TradeTime :anydttme. Ticker :$4. Qty :best10.; cards; 1Aug2013 10:00 MSFT 1000 1Aug2013 10:02 MSFT 1002 1Aug2013 14:20 MSFT 1420 1Aug2013 15:59 MSFT 1559 2Aug2013 09:31 MSFT 0931 2Aug2013 12:01 MSFT 1201 2Aug2013 15:20 MSFT 1520 2Aug2013 15:21 MSFT 1521 1Aug2013 10:00 YHOO 1000 1Aug2013 10:06 YHOO 1006 1Aug2013 14:21 YHOO 1421 1Aug2013 15:58 YHOO 1558 2Aug2013 09:30 YHOO 0930 2Aug2013 12:01 YHOO 1201 2Aug2013 15:20 YHOO 1520 2Aug2013 15:27 YHOO 1527 1Aug2013 10:00 AAPL 1000 1Aug2013 10:06 AAPL 1006 1Aug2013 14:21 AAPL 1421 1Aug2013 15:58 AAPL 1558 2Aug2013 09:30 AAPL 0930 2Aug2013 12:01 AAPL 1201 2Aug2013 15:20 AAPL 1520 2Aug2013 15:27 AAPL 1527 ; run; proc sort data=Events; by Ticker TradeDate TradeTime; data Prices; format TradeDate mmddyys10. TradeTime time.; input TradeDate :anydtdte. TradeTime :anydttme. Ticker :$4. Price :best10.; cards; 1Aug2013 09:30 MSFT 09.30 1Aug2013 10:01 MSFT 10.01 1Aug2013 10:02 MSFT 10.02 1Aug2013 11:59 MSFT 11.59 2Aug2013 09:30 MSFT 09.30 2Aug2013 10:01 MSFT 10.01 2Aug2013 15:18 MSFT 15.18 2Aug2013 15:19 MSFT 15.19 2Aug2013 15:20 MSFT 15.20 2Aug2013 15:21 MSFT 15.21 1Aug2013 10:01 YHOO 10.01 1Aug2013 10:05 YHOO 10.05 2Aug2013 09:28 YHOO 09.28 2Aug2013 09:29 YHOO 09.29 2Aug2013 11:00 YHOO 11.00 2Aug2013 15:25 YHOO 15.25 2Aug2013 16:00 YHOO 16.00 ; run; proc sort data=Prices; by Ticker TradeDate TradeTime; proc print data=Events; proc print data=Prices; /* too slow on big data */ proc sql; select a.*, b.TradeTime as PriceTime, b.Price from Events a join Prices b on a.TradeDate = b.TradeDate and a.Ticker = b.Ticker and b.TradeTime < a.TradeTime group by a.TradeDate, a.Ticker, a.TradeTime having b.TradeTime = max( b.TradeTime) order by a.Ticker, a.TradeDate, a.TradeTime; Here are the results: TradeDate TradeTime Ticker Qty PriceTime Price 08/01/2013 10:00:00 MSFT 1000 9:30:00 9.3 08/01/2013 10:02:00 MSFT 1002 10:01:00 10.01 08/01/2013 14:20:00 MSFT 1420 11:59:00 11.59 08/01/2013 15:59:00 MSFT 1559 11:59:00 11.59 08/02/2013 9:31:00 MSFT 931 9:30:00 9.3 08/02/2013 12:01:00 MSFT 1201 10:01:00 10.01 08/02/2013 15:20:00 MSFT 1520 15:19:00 15.19 08/02/2013 15:21:00 MSFT 1521 15:20:00 15.2 08/01/2013 10:06:00 YHOO 1006 10:05:00 10.05 08/01/2013 14:21:00 YHOO 1421 10:05:00 10.05 08/01/2013 15:58:00 YHOO 1558 10:05:00 10.05 08/02/2013 9:30:00 YHOO 930 9:29:00 9.29 08/02/2013 12:01:00 YHOO 1201 11:00:00 11 08/02/2013 15:20:00 YHOO 1520 11:00:00 11 08/02/2013 15:27:00 YHOO 1527 15:25:00 15.25
... View more