<?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 Re: Cumulative Sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780115#M248542</link>
    <description>&lt;P&gt;Only use first.date. A change in a variable to the left of date in the BY (higher up in the hierarchy) will also cause a change for date, even when&amp;nbsp;&lt;EM&gt;that&lt;/EM&gt; value is the same.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_car;
set dataset_abret;
by permno evtdate date;
if first.date 
then car = abret;
else car + abret;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM Statement will cause an automatic RETAIN.&lt;/P&gt;</description>
    <pubDate>Sat, 13 Nov 2021 16:12:05 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2021-11-13T16:12:05Z</dc:date>
    <item>
      <title>Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780023#M248490</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some issues to compute some Cumulative Sum. I have a dataset organized with some variables, and I have grouped all the different results according to three different variables (in the following order): PERMNO, EVTDATE, DATE.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to compute the Cumulative Sum for another variable of interest. i was thinking to use an if first. to do all my computations, setting it equal to the variable itself for the first value and then cumulate it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Still, this does not function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May I ask someone to help me with this, or if any suggestion?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 Nov 2021 18:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780023#M248490</guid>
      <dc:creator>RDellaVilla</dc:creator>
      <dc:date>2021-11-12T18:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780029#M248492</link>
      <description>Approach seems correct, please post the code so we can help you out.</description>
      <pubDate>Fri, 12 Nov 2021 19:00:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780029#M248492</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-12T19:00:06Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780048#M248500</link>
      <description>&lt;P&gt;"Does not function" is awful vague.&lt;BR /&gt;&lt;BR /&gt;Are there errors in the log?: Post the code and log in a code box opened with the "&amp;lt;/&amp;gt;" to maintain formatting of error messages.&lt;BR /&gt;&lt;BR /&gt;No output? Post any log in a code box.&lt;BR /&gt;&lt;BR /&gt;Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the "&amp;lt;/&amp;gt;" icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Nov 2021 21:12:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780048#M248500</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-12T21:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780111#M248538</link>
      <description>&lt;P&gt;Thanks, I here paste the Code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"data dataset_car;&lt;BR /&gt;set dataset_abret;&lt;BR /&gt;by permno evtdate date;&lt;BR /&gt;retain car;&lt;BR /&gt;if first.permno or (first.permno and first.evtdate) then&lt;BR /&gt;car = abret;&lt;BR /&gt;else car = car+abret;&lt;BR /&gt;run;"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thing is, I have multiple evtdate and date per permno, so I need to cumulate my results based on this sequence: PERMNO,EVTDATE,DATE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you could help, that would be much appreciated!&lt;/P&gt;</description>
      <pubDate>Sat, 13 Nov 2021 14:51:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780111#M248538</guid>
      <dc:creator>RDellaVilla</dc:creator>
      <dc:date>2021-11-13T14:51:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780112#M248539</link>
      <description>&lt;P&gt;The specific request was for you to provide the LOG, which includes the code plus any ERRORs, WARNINGs and NOTEs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And we also would need to see a portion of the data, and incorrect output, if that is the problem. But really, you have not yet explained what the problem is ... explain, please.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Nov 2021 14:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780112#M248539</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-13T14:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780113#M248540</link>
      <description>&lt;DIV class=""&gt;So as for the &lt;STRONG&gt;Log&lt;/STRONG&gt;, no mistake is shown/nor warning message: "&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;427        *Cumulative Abnormal Returns and Gross Return per PERMNO;
 428        
 429        data dataset_car;
 430        set dataset_abret;
 431        by permno evtdate date;
 432        retain car;
 433        if first.permno or (first.permno and first.evtdate) then
 434        car = abret;
 435        else car=car+abret;
 436        run;
 
 NOTE: There were 171237 observations read from the data set WORK.DATASET_ABRET.
 NOTE: The data set WORK.DATASET_CAR has 171237 observations and 12 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.03 seconds
       cpu time            0.04 seconds&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;Following the procedure to make the &lt;STRONG&gt;output&lt;/STRONG&gt; compatible with the forum, here is attached a much filtered version of the output. Looking at the highlighted values, it seems that my procedure functions for the very first value associated with the Permno, but it does not change with the second layer (in bold): first value associated to the new Event Date. It keeps cumulating the results according to permno and does not stop at the new Event Date.&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;Permno EVTDate&amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Abret&amp;nbsp; &amp;nbsp; &amp;nbsp;Cumulative Abret&amp;nbsp;
&lt;STRONG&gt;10051 24JUL2020 23JUL2020 -0.001997522 -0.001997522&lt;/STRONG&gt;
10051 24JUL2020 24JUL2020 -0.008036756 -0.010034278
10051 24JUL2020 27JUL2020 -0.010701593 -0.020735871
[....] here is all the data up to the end of the time window around the Evtd Date&amp;nbsp;
10051 24JUL2020 04SEP2020 -0.016240639 0.0344095798
&lt;STRONG&gt;10051 27JUL2020 23JUL2020 -0.001997522 0.032412058&lt;/STRONG&gt;
10051 27JUL2020 24JUL2020 -0.008036756 0.0243753022
[...] same&amp;nbsp; here, I have deleted all the intermediate results up to the end of the 2nd event date.
10051 27JUL2020 08SEP2020 -0.008131352 0.0606878076
&lt;STRONG&gt;10051 31JUL2020 23JUL2020 -0.001997522 0.0586902858&lt;/STRONG&gt;&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 13 Nov 2021 15:51:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780113#M248540</guid>
      <dc:creator>RDellaVilla</dc:creator>
      <dc:date>2021-11-13T15:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780114#M248541</link>
      <description>&lt;P&gt;First thing is that if it is FIRST.PERMNO it is by definition also FIRST.EVTDATE because you have nested EVTDATE inside PERMNO.&amp;nbsp; So your test for a new group is just going to be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.evtdate then car = abret;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Make sure that CAR is not already on the input dataset. If it is then the SET statement will overwrite any value carried forward by the retaining the variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could use a different variable name for your new variable. Or use the DROP= dataset option to remove the existing variable CAR.&amp;nbsp; Or use RENAME= to change its name to something else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_car;
  set dataset_abret(drop=car);
  by permno evtdate date;
  car+abret;
  if first.evtdate then car = abret;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 Nov 2021 16:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780114#M248541</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-13T16:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780115#M248542</link>
      <description>&lt;P&gt;Only use first.date. A change in a variable to the left of date in the BY (higher up in the hierarchy) will also cause a change for date, even when&amp;nbsp;&lt;EM&gt;that&lt;/EM&gt; value is the same.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dataset_car;
set dataset_abret;
by permno evtdate date;
if first.date 
then car = abret;
else car + abret;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM Statement will cause an automatic RETAIN.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Nov 2021 16:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum/m-p/780115#M248542</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-13T16:12:05Z</dc:date>
    </item>
  </channel>
</rss>

