<?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 count the number of trading days in the past 3 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488418#M127304</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to count the number of trading days in the past 3 months, using CRSP daily data ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Aug 2018 03:31:53 GMT</pubDate>
    <dc:creator>Nieves</dc:creator>
    <dc:date>2018-08-21T03:31:53Z</dc:date>
    <item>
      <title>count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488418#M127304</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How to count the number of trading days in the past 3 months, using CRSP daily data ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 03:31:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488418#M127304</guid>
      <dc:creator>Nieves</dc:creator>
      <dc:date>2018-08-21T03:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488419#M127305</link>
      <description>&lt;P&gt;You're extremely lucky to have a WRDS consultant on this forum - me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I take it you really mean the number of days the market was active, rather than the number of days a particular stock traded, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does "last 3 months" mean?&amp;nbsp; Last three calendar months?&amp;nbsp;&amp;nbsp; If the current trading date is in the middle of August, then do you want the number of trading days in May, June, and July?&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 03:59:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488419#M127305</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-21T03:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488426#M127306</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nice to hear from a wrds expert. I am calculating the sigma ratio defined as follows: daily variation of returns&amp;nbsp;computed as an annualized 3-month rolling sample standard deviation, using daily stock price data from CRSP.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If there are fewer than five nonzero observations over the 3 months used in the rolling window computation, SIGMA will be counted as missing and replace with the cross-sectional mean of SIGMA.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sigma.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22645iB9A0807FBE481F8D/image-size/large?v=v2&amp;amp;px=999" role="button" title="sigma.JPG" alt="sigma.JPG" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Therefore, I need the number of days a particular stock traded,&amp;nbsp;rather than the number of days the market was active.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for each date t, I need to calculate the number of trading days for each stock&amp;nbsp;for the past three calendar month of the date t.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes.&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;If the current trading date is in the middle of August,&amp;nbsp;I&amp;nbsp;want the number of trading days in May, June, and July for each stock in the data.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Looking forward to your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 05:09:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488426#M127306</guid>
      <dc:creator>Nieves</dc:creator>
      <dc:date>2018-08-21T05:09:18Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488638#M127392</link>
      <description>&lt;P&gt;OK, I get that r-square sub (i,k) is r-square for the i'th stock and k'th month.&amp;nbsp;&amp;nbsp; But what is the model from which each monthly r-square is calculated?&amp;nbsp; Is it the capm value or something else?&amp;nbsp; Where are you getting the r-square?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More generally, regarding you initial question, here's how to get the number of trading days in the prior three calendar months:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set dsf;
  by permno;
  retain trdays_3months;

  if dif(month(date))^=0 or first.permno=1 then trdays_3months=ifn(dif3(permno)=0,dif3(_n_),.);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Assumes that the daily stock file is sorted by permno, date.&lt;/LI&gt;
&lt;LI&gt;The "dif(x)" function&amp;nbsp;is defined as&amp;nbsp;x-lag(x).&amp;nbsp; So the if clause tests whether the record in hand is the beginning of a new month, or beginning of a new permno.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;The "then" clause calculates the dif3(_n_), same as _n_-lag3(_n_), which means the current observation number minus the observation number three instances ago.&amp;nbsp;&amp;nbsp;I say three instances ago&amp;nbsp;instead of three observations ago&amp;nbsp;because the dif3 function (like the lag3 function) is a queue-management function,&amp;nbsp;which is updated only when the IF clause is true.&amp;nbsp; This&amp;nbsp;means it's the difference in _N_ between current record and the 3rd prior time a month change was encountered.&amp;nbsp; And _N_ in this program is the observation number.&amp;nbsp; I.e, this is NOT like a look back x rows you might do in Excel.&lt;BR /&gt;&lt;BR /&gt;However you don't want to contaminate the result of this statement when processing the first 3 months of a new permno.&amp;nbsp; So there is a test of dif3(permno) such that a missing value is assigned to trdays_3months during the first 3 months of a new permno.&lt;/LI&gt;
&lt;LI&gt;The "retain trdays_3months" statement prevents SAS from automatically resetting new variables to a missing at the start of each iteration of the data step.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited additional note:&lt;/P&gt;
&lt;P&gt;This program assume that each stock has at least one trading day per month.&amp;nbsp; This is, if a stock is delisted for at least one month, then the period after relisting will have erroneous values for the first 3 months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 17:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488638#M127392</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-21T17:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488765#M127435</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your kind help and detailed advice. Your program works!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The return here is the (price-price_lag)/price_lag&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In addition to your program, may I also ask how to implement the additional requirement ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if there are fewer than five nonzero observations over the prior 3 calendar months used in the rolling window computation (which is&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;trdays_3months &amp;lt; 5)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;, SIGMA will be counted as missing and replace with the cross-sectional mean of SIGMA. I have already calculated SIGMA in my data and set as missing if less than 5 nonzero observations over the prior 3 calendar months.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Aug 2018 04:12:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/488765#M127435</guid>
      <dc:creator>Nieves</dc:creator>
      <dc:date>2018-08-22T04:12:17Z</dc:date>
    </item>
    <item>
      <title>Re: count the number of trading days in the past 3 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/489296#M127731</link>
      <description>&lt;P&gt;Regarding setting sigma to missing when trdays_3months&amp;lt;5, I wouldn't shift implementing that rule to the calculation of trdays_3months.&amp;nbsp; I'd keep it right where you apparently have it - when you calculate sigma.&amp;nbsp; BUT ... you can do that in the same step as in this code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=m);
  set dsf;
  by permno;
  retain trdays_3months 0  m 0;

  /* To hold three separate sums of monthly squared returns */
  array mretsq{0:2} _temporary_  (3*0);

  if dif(month(date))^=0 or first.permno=1 then do;
    trdays_3months=ifn(dif3(permno)=0,dif3(_n_),.);

    retsquare_3months=sum(of mretsq{*});
    if trdays_3months&amp;gt;=5 then sigma=sqrt( (252/(N-1))*retsquare_3months  );

    /* Point to next element of array mretsq, and zero it out */
    m=mod(m+1,3);
    mretsq{m}=0;
  end;

  mretsq{m}+ret**2;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code declare a 3-element temporary array MRETSQ, to hold sum-of-squared-returns for each of the last three months.&amp;nbsp; It's a temporary array, so (1) no new variables are output to the new data set, and (2) values are automatically retained for record to record.&amp;nbsp; The variable m is used to identify which element of the array to add the current record (element 0, element 1, or element 2).&amp;nbsp; But the actual adding of the current record to the monthly total takes place AFTER the code for discovering a change in month.&amp;nbsp; That's because you want to generate sigma for prior three months without contamination from the current record (i.e. the first record of the new month).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the&amp;nbsp;if/then statement is now the same IF test, but has a "then do" group instead of a single "then" action.&amp;nbsp; The do group includes calculation of two new variables: retsquare_3months, and sigma.&amp;nbsp; And it also rotates M to the next mretsq element, by using the mod function.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 15:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-the-number-of-trading-days-in-the-past-3-months/m-p/489296#M127731</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-08-23T15:02:32Z</dc:date>
    </item>
  </channel>
</rss>

