<?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 Cumulative returns for different time windows in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672234#M23411</link>
    <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This is my very first post to this forum, I hope I can ask my question while abiding by the forum rules.&lt;BR /&gt;&lt;BR /&gt;I have an unbalanced panel data set of daily security log returns. This data set is constructed by merging two data sets: security return data (firm id is gvkey) and another data set containing firm-specific information (firm id is gvkey+iid). Each observation has two dates (date and date2) together with a dummy variable and I'd like to calculate the cumulative log returns between these two dates for those having dummy=1. I'd also like to note that the data is not uniquely defined in the gvkey-date pair since the data set containing firm characteristics has a more disaggregated firm id definition (gvkey+iid). Below, I included a screenshot of the part of my data set:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47655i72D4D0C3FA5FE052/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sample.png" alt="sample.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;What I want to do is to calculate the cumulative returns between date and date2 only for row numbers 301, 302, and 310 (where dummy=1). Below screenshot displays what I am trying to get in the end:&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample2.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47659iDFE848AE8F7193A4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sample2.png" alt="sample2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Just to make it clearer, the cumulative return for row 310 is calculated as the sum of log-returns shown within red rectangles (or, the sum of single daily log returns from 12/21/2010 to 12/31/2010). Of course, this is just a sample data set, the original data has millions of observations with multiple gvkeys and a larger time frame.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some experience with SAS but I couldn't think a clever way to tackle this specific problem. I'd appreciate your help!&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jul 2020 22:35:01 GMT</pubDate>
    <dc:creator>anildonmez</dc:creator>
    <dc:date>2020-07-24T22:35:01Z</dc:date>
    <item>
      <title>Cumulative returns for different time windows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672234#M23411</link>
      <description>&lt;P&gt;Hi SAS community,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This is my very first post to this forum, I hope I can ask my question while abiding by the forum rules.&lt;BR /&gt;&lt;BR /&gt;I have an unbalanced panel data set of daily security log returns. This data set is constructed by merging two data sets: security return data (firm id is gvkey) and another data set containing firm-specific information (firm id is gvkey+iid). Each observation has two dates (date and date2) together with a dummy variable and I'd like to calculate the cumulative log returns between these two dates for those having dummy=1. I'd also like to note that the data is not uniquely defined in the gvkey-date pair since the data set containing firm characteristics has a more disaggregated firm id definition (gvkey+iid). Below, I included a screenshot of the part of my data set:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47655i72D4D0C3FA5FE052/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sample.png" alt="sample.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;What I want to do is to calculate the cumulative returns between date and date2 only for row numbers 301, 302, and 310 (where dummy=1). Below screenshot displays what I am trying to get in the end:&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sample2.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/47659iDFE848AE8F7193A4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="sample2.png" alt="sample2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Just to make it clearer, the cumulative return for row 310 is calculated as the sum of log-returns shown within red rectangles (or, the sum of single daily log returns from 12/21/2010 to 12/31/2010). Of course, this is just a sample data set, the original data has millions of observations with multiple gvkeys and a larger time frame.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some experience with SAS but I couldn't think a clever way to tackle this specific problem. I'd appreciate your help!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jul 2020 22:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672234#M23411</guid>
      <dc:creator>anildonmez</dc:creator>
      <dc:date>2020-07-24T22:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative returns for different time windows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672258#M23414</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/325327"&gt;@anildonmez&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have an unbalanced panel data set of daily security log returns. This data set is constructed by merging two data sets: &lt;STRONG&gt;security return data (firm id is gvkey)&lt;/STRONG&gt; and another data set containing &lt;STRONG&gt;firm-specific information (firm id is gvkey+iid).&lt;/STRONG&gt; Each observation has two dates (date and date2) together with a dummy variable and I'd like to calculate the cumulative log returns between these two dates for those having dummy=1. I'd also like to note that the data is not uniquely defined in the gvkey-date pair since the data set containing firm characteristics has a more disaggregated firm id definition (gvkey+iid). Below, I included a screenshot of the part of my data set:&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First things first:&lt;/P&gt;
&lt;P&gt;In Compustat GVKEY is the unique firm id, and IID is the issue id within that firm.&amp;nbsp; So gvkey+iid is essentially the unique identifier for a stock issue - it's not really a firm id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As far as I can tell from the boxes you drew, for each record with dummy=1 you want to aggregate the log returns for all dates between DATE and DATE2 in which dummy is not a zero:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let begdate=01jan2010;
%let enddate=31dec2010;

data want (drop=_:);
  array ret_history {%sysevalf("&amp;amp;begdate"d):%sysevalf("&amp;amp;enddate"d)} _temporary_;
  call missing(of ret_history{*});

  do until (last.gvkey);
    set have;
    by gvkey;
    if dummy ^=0 then ret_history{date}=log_ret;
  end;

  do until(last.gvkey);
    set have;
	by gvkey;
	cum_ret=.;
	if dummy=1 then do _d=date to date2;
	  cum_ret=sum(cum_ret,ret_history{_d});
	end;
    output;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you know the date range of your data, then set the macrovars BEGDATE and ENDDATE accordingly.&amp;nbsp; This allows the definition of an array RET_HISTORY, indexed by date.&amp;nbsp; For each GVKEY, RET_HISTORY is initialized to missing, and then each of the dates with dummy^=0 has its return put in ret_history.&amp;nbsp;&amp;nbsp; That's the first pass of each gvkey, just to establish ret_history.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then reread the gvkey.&amp;nbsp; Each time a dummy=1 aggregate the log_ret values from ret_history from date to date2, followed by an output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note this assumes that any date with dummy=.&amp;nbsp; or dummy=0 has only a single unique value for log_ret.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jul 2020 03:43:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672258#M23414</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-25T03:43:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative returns for different time windows</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672717#M23476</link>
      <description>Thanks a lot, it worked just great!</description>
      <pubDate>Mon, 27 Jul 2020 23:50:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Cumulative-returns-for-different-time-windows/m-p/672717#M23476</guid>
      <dc:creator>anildonmez</dc:creator>
      <dc:date>2020-07-27T23:50:01Z</dc:date>
    </item>
  </channel>
</rss>

