<?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 Mean of respective month in the last 2-5 years in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685292#M207823</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which includes many firms. For each firm, there many years. There are only 2 months June and August in each firm-year. For each firm-year-month June (August), I would like to obtain the means of excess return (exret) of the same months and different months in the last 2 to 5 years (year t-2 to t-5).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, first, for firm 1-June 2019, I would like to get the mean of (June 2018, June 2017,...., June 2014). Second, for firm 1-June 2019, I also would like to get the mean of (August 2018,August 2017,....,August 2014). Some firms do not have data for all last 2-5 years. (please see the screenshot of the dataset). I think that I need to run 2 SQL commands, first I tried the codes below to get the mean of same months in the last 2-5 years but unsuccessful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"&lt;FONT color="#0000FF"&gt;proc sql;&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;create table&lt;/FONT&gt; return_lag_25_c&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;as select&lt;/FONT&gt; a.*, &lt;STRONG&gt;mean(b.exret) as mean_25_c&lt;/STRONG&gt; /* obtain the mean of excess return*/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;from&lt;/FONT&gt; price_month2 as a, price_month2 as b&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;FONT color="#0000FF"&gt;&amp;nbsp;where&lt;/FONT&gt; a.tcode=b.tcode and a.ltd_mo=b.ltd_mo and 2 &amp;lt;= a.ltd_yr - b.ltd_yr&amp;lt;= 5 /* from the last year t-2 to t-5*/&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;group by&lt;/FONT&gt; a.tcode, a.ltd_yr, a.ltd_mo;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;quit;&lt;/FONT&gt; "&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* It is not working-200920. there are 233606 rows in the output while there are only 110,330 in the input dataset price_month2*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that any one can share with me some tips to resolve this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;</description>
    <pubDate>Mon, 21 Sep 2020 00:20:49 GMT</pubDate>
    <dc:creator>chriscpa</dc:creator>
    <dc:date>2020-09-21T00:20:49Z</dc:date>
    <item>
      <title>Mean of respective month in the last 2-5 years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685292#M207823</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which includes many firms. For each firm, there many years. There are only 2 months June and August in each firm-year. For each firm-year-month June (August), I would like to obtain the means of excess return (exret) of the same months and different months in the last 2 to 5 years (year t-2 to t-5).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, first, for firm 1-June 2019, I would like to get the mean of (June 2018, June 2017,...., June 2014). Second, for firm 1-June 2019, I also would like to get the mean of (August 2018,August 2017,....,August 2014). Some firms do not have data for all last 2-5 years. (please see the screenshot of the dataset). I think that I need to run 2 SQL commands, first I tried the codes below to get the mean of same months in the last 2-5 years but unsuccessful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"&lt;FONT color="#0000FF"&gt;proc sql;&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;create table&lt;/FONT&gt; return_lag_25_c&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;as select&lt;/FONT&gt; a.*, &lt;STRONG&gt;mean(b.exret) as mean_25_c&lt;/STRONG&gt; /* obtain the mean of excess return*/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;from&lt;/FONT&gt; price_month2 as a, price_month2 as b&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;FONT color="#0000FF"&gt;&amp;nbsp;where&lt;/FONT&gt; a.tcode=b.tcode and a.ltd_mo=b.ltd_mo and 2 &amp;lt;= a.ltd_yr - b.ltd_yr&amp;lt;= 5 /* from the last year t-2 to t-5*/&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;FONT color="#0000FF"&gt;group by&lt;/FONT&gt; a.tcode, a.ltd_yr, a.ltd_mo;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;quit;&lt;/FONT&gt; "&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* It is not working-200920. there are 233606 rows in the output while there are only 110,330 in the input dataset price_month2*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that any one can share with me some tips to resolve this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Mon, 21 Sep 2020 00:20:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685292#M207823</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-21T00:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: Mean of respective month in the last 2-5 years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685311#M207833</link>
      <description>&lt;P&gt;You can do it in one query, getting the means as subqueries (not tested):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table return_lag_25_c
  as select main.*, 
     (select mean(exret)  from price_month2 as b 
       where tcode=main.tcode 
         and ltd_mo=main.ltd_mo
         and ltd_yr between main.ltd_yr-5 and main.ltd_yr-2)                 
       as mean_same_month,     
     (select mean(exret)  from price_month2 as b 
       where tcode=main.tcode 
         and ltd_mo ne main.ltd_mo
         and ltd_yr between main.ltd_yr-5 and main.ltd_yr-2)                 
       as mean_different_month    
  from price_month2 as main
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Sep 2020 06:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685311#M207833</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-09-21T06:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Mean of respective month in the last 2-5 years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685344#M207845</link>
      <description>&lt;P&gt;Dear Lassen,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It worked like a charm!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Mon, 21 Sep 2020 08:57:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/685344#M207845</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-21T08:57:20Z</dc:date>
    </item>
    <item>
      <title>Re: Mean of respective month in the last 2-5 years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/686981#M208522</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The codes is working perfectly for monthly data. However, when I applied them for daily data and the mean is estimated from lag data t-20 to t-6, it took days to run the codes. If there any way that we can reduce the running time?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;</description>
      <pubDate>Sun, 27 Sep 2020 01:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Mean-of-respective-month-in-the-last-2-5-years/m-p/686981#M208522</guid>
      <dc:creator>chriscpa</dc:creator>
      <dc:date>2020-09-27T01:31:38Z</dc:date>
    </item>
  </channel>
</rss>

