<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Speed up Custom &amp;quot;trading day&amp;quot; Interval in INTCK function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438030#M109174</link>
    <description>&lt;P&gt;I created a custom interval for trading days using the following code:&lt;/P&gt;
&lt;PRE&gt;* code to create trading day intervals;
proc sort data = dat.trading_day0;by caldt;run;
data trading_days (rename = (lag1dt=begin caldt=end)); set dat.trading_day0;
format lag1dt caldt date9.;
lag1dt = lag(caldt);run;

options intervalds=(TradingDay=work.trading_days);
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pretty straightforward. But when I want to use it to compute trading day intervals, it is *really* slow.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I run the following:&lt;/P&gt;
&lt;PRE&gt;data test1; set dat.neg_events1;
	trday_gap_lag = intck('TradingDay',TradingDate,lag1date);
run;
data test1; set test1;
	weekday_gap_lag = intck('weekday',TradingDate,lag1date);
run;&lt;/PRE&gt;
&lt;P&gt;The first data step takes 25 seconds, the second one takes 0.02 seconds. This is just a subsample of my data. On the full dataset, the first step takes almost 3 minutes, the second takes 0.25 seconds. Yet they are (almost) identical. Clearly there is some optimization on the second one that I don't have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both necessary datasets attached (trading_day0 and neg_events1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Feb 2018 17:39:33 GMT</pubDate>
    <dc:creator>jab</dc:creator>
    <dc:date>2018-02-16T17:39:33Z</dc:date>
    <item>
      <title>Speed up Custom "trading day" Interval in INTCK function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438030#M109174</link>
      <description>&lt;P&gt;I created a custom interval for trading days using the following code:&lt;/P&gt;
&lt;PRE&gt;* code to create trading day intervals;
proc sort data = dat.trading_day0;by caldt;run;
data trading_days (rename = (lag1dt=begin caldt=end)); set dat.trading_day0;
format lag1dt caldt date9.;
lag1dt = lag(caldt);run;

options intervalds=(TradingDay=work.trading_days);
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pretty straightforward. But when I want to use it to compute trading day intervals, it is *really* slow.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I run the following:&lt;/P&gt;
&lt;PRE&gt;data test1; set dat.neg_events1;
	trday_gap_lag = intck('TradingDay',TradingDate,lag1date);
run;
data test1; set test1;
	weekday_gap_lag = intck('weekday',TradingDate,lag1date);
run;&lt;/PRE&gt;
&lt;P&gt;The first data step takes 25 seconds, the second one takes 0.02 seconds. This is just a subsample of my data. On the full dataset, the first step takes almost 3 minutes, the second takes 0.25 seconds. Yet they are (almost) identical. Clearly there is some optimization on the second one that I don't have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both necessary datasets attached (trading_day0 and neg_events1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Feb 2018 17:39:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438030#M109174</guid>
      <dc:creator>jab</dc:creator>
      <dc:date>2018-02-16T17:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: Speed up Custom "trading day" Interval in INTCK function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438330#M109270</link>
      <description>&lt;P&gt;That's the way it is. The customer interval is not optimised (precompiled) so it runs slower.&lt;/P&gt;
&lt;P&gt;I divided the run time by three by using&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;sasfile TRADING_DAYS  load;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you must use custom intervals, make sure you have as few intervals as possible in that table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 03:08:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438330#M109270</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-02-19T03:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: Speed up Custom "trading day" Interval in INTCK function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438431#M109317</link>
      <description>&lt;P&gt;Ok. That is helpful in terms of understanding and somewhat with speed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, to your last point: Can I omit all 1 day entries then? I.e. I only need to include entries with weekends and the days that markets are closed? That would allow me to omit a lot of entries. The documentation wasn't clear to me: it said if there was no end date, it would just increment by 1, but I wasn't sure what the behavior would be if there were omissions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Related: Is there a way to have my custom list "inherit" the 'weekday' specification? Then, I could just have a handful of entries per year for holidays when markets were closed.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally: is there a way to make this a feature request? I imagine I'm not the only one doing research on financial markets who would use this feature. Getting a 'trading day' interval precompiled would be super helpful because financial market data can be quite large and therefore slow.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 16:34:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438431#M109317</guid>
      <dc:creator>jab</dc:creator>
      <dc:date>2018-02-19T16:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Speed up Custom "trading day" Interval in INTCK function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438452#M109323</link>
      <description>&lt;P&gt;See if a hash lookup&amp;nbsp;is any faster:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data = dat.trading_day0; by caldt; run;

data trading_day_index (rename=(caldt=_caldt));
  set dat.trading_day0;
  _index=_n_;
run;


data test1 (drop=_:);
  set dat.neg_events1;
  if _n_=1 then do;
    if 0 then set trading_day_index;
    declare hash h (dataset:'trading_day_index');
      h.definekey('_caldt');
      h.definedata('_index');
      h.definedone();
  end;

  do _d=tradingdate,lag1date;
    rc=h.find(key:_d);
    trday_gap_hash=dif(_index);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Make sure that dat.trading_day0 covers all the values of tradingdate and lag1date.&lt;/LI&gt;
&lt;LI&gt;The "do _d=..." loop is used so that the DIF function it contains is a single dif QUEUE, even though two unique values (tradingdate, and lag1date) are submitted to that queue.&amp;nbsp; In other words this would fail:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc=h.find(key:tradingdate);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trday_gap_hash=dif(_index);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc=h.find(key:lag1date);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trday_gap_hash=dif(_index);&lt;BR /&gt;because two independent DIF queues would be generated.&lt;/LI&gt;
&lt;LI&gt;If the DIF queue explanation is not sufficiently clear, then you could:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc=h.find(key:tradingdate);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _index_from=_index;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rc=h.find(key:lag1date);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; trday_gap_hash=_index-_index_from;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 19 Feb 2018 18:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438452#M109323</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-02-19T18:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: Speed up Custom "trading day" Interval in INTCK function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438467#M109327</link>
      <description>&lt;P&gt;Yes, a trading date calendar would be great.&amp;nbsp; But which exchange(s)?&amp;nbsp; NYSE? LSE?&amp;nbsp; Bourse? etc.&amp;nbsp;&amp;nbsp; Or maybe you would want global trading dates covering multiple exchanges?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And how far back should the calendars go?&amp;nbsp; All the normal sas calendar intervals can be relied on to accurately go back to the start of the Gregorian calendar (October 1582).&amp;nbsp; But each exchange would have a unique "birthdate", so there would be no common starting date.&amp;nbsp; Maybe such a function would only accept dates after 01jan1900.&amp;nbsp; But then there are younger exchanges to be accommodated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, and likely more problematic: Normal SAS calendar intervals can be relied on for accurate future dates and intervals, but we don't know the future calendars of stock markets.&amp;nbsp; Not only do exchanges change their calendars by policy, they can also miss dates through unanticipated events.&amp;nbsp;&amp;nbsp;On 9/11/2001 NYSE was closed early, and was closed all day on Tuesday 9/12 through Friday 9/15.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, you might restrict such functions to reject arguments with future dates.&amp;nbsp; But that would not help when running such a function on 9/17/2001 (the NYSE reopen date).&amp;nbsp; It would regard the prior trading date as 9/15.&amp;nbsp; After all we don't update pre-compiled sas functions on a daily basis.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And as to speed: sas calendar functions using normal intervals ('day','week','weekday',etc.) are all algorithm based.&amp;nbsp; While functions using holidays, (or trading days) have to be based on a date-list instead of an algorithm.&amp;nbsp; I don't think we'll ever get functions using&amp;nbsp; trading-day intervals to be as fast as regular calendar units.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited note: In so far as holiday assignments are rule-based and not idiosyncratic from year to year, you might be able to improve speed.&amp;nbsp; But holidays - like trading days - can change over time.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 22:27:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Speed-up-Custom-quot-trading-day-quot-Interval-in-INTCK-function/m-p/438467#M109327</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-02-19T22:27:41Z</dc:date>
    </item>
  </channel>
</rss>

