<?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: Multiplicative Acumulation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553012#M153752</link>
    <description>&lt;P&gt;Doesn't work 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 {i} 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-dat" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat&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 {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Apr 2019 19:24:14 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-04-22T19:24:14Z</dc:date>
    <item>
      <title>Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553010#M153751</link>
      <description>&lt;P&gt;Suppose a stock return panel data set as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data RAW;
format FIRM $8. DATE yymmddn8.;
do FIRM="GOOGL","MSFT";
do DATE="12apr2019"d to "22apr2019"d;
RETURN=round(0.01+sqrt(0.01)*rannor(1),0.01);
output;
end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;In many cases, I create a cumulative return by (1) using a log return, (2) accumulating, and (3) taking an exponential. I tried to shorten these tasks using IFN, but failed. Is there any shorter way to do the multiplicative works?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PROCESSED;
set RAW;
by FIRM DATE;

/*1. Conventional. Works nicely, but too many lines*/
if first.FIRM then LOG=log(1+RETURN);
else LOG+log(1+RETURN);
CUMULATIVE=exp(LOG)-1;

/*2. Tried IFN to shorten, but doesn't work*/
LOG1=ifn(first.FIRM,log(1+RETURN),LOG1+log(1+RETURN));

/*3. Wondered if it's about LAG, but wasn't*/
LOG2=ifn(first.FIRM,log(1+RETURN),lag(LOG2)+log(1+RETURN));

/*4. Tried to accumulate by multiplying, but doesn't work*/
if first.FIRM then CUMULATIVE1=RETURN;
else CUMULATIVE1=(1+CUMULATIVE1)*(1+RETURN)-1;

/*5. Wondered if it's about LAG, but wasn't*/
if first.FIRM then CUMULATIVE2=RETURN;
else CUMULATIVE2=(1+lag(CUMULATIVE2))*(1+RETURN)-1;

/*6. Tried to shorten using IFN, but doesn't work*/
CUMULATIVE3=ifn(first.FIRM,RETURN,(1+CUMULATIVE3)*(1+RETURN)-1);

/*7. Tried the same thing with a LAG function, but doesn't work*/
CUMULATIVE4=ifn(first.FIRM,RETURN,(1+lag(CUMULATIVE4))*(1+RETURN)-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 19:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553010#M153751</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-22T19:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553012#M153752</link>
      <description>&lt;P&gt;Doesn't work 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 {i} 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-dat" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat&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 {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 19:24:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553012#M153752</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-22T19:24:14Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553015#M153754</link>
      <description>Do you have SAS ETS licensed? Have you tried PROC EXPAND?</description>
      <pubDate>Mon, 22 Apr 2019 19:27:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553015#M153754</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-04-22T19:27:23Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553028#M153756</link>
      <description>&lt;P&gt;#1 is very short.&amp;nbsp; Why is it too many lines?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are attempting to speed up the execution, get rid of one LOG function.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.firm then do;
   increment = log(1 + RETURN);
   log = increment;
   retain increment;
end;
else log + increment;
CUMULATIVE = exp(LOG) - 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'm not sure whether the math is correct or not, but it is equivalent (generates the same result as #1).&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 19:47:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553028#M153756</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-04-22T19:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553035#M153757</link>
      <description>&lt;P&gt;Your approach #2 would benefit from a RETAIN statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain log1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Apr 2019 20:00:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553035#M153757</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-04-22T20:00:48Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplicative Acumulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553054#M153762</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.png" style="width: 128px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28923iC320B81AADEBF5F5/image-size/small?v=v2&amp;amp;px=200" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28922iE8B6224692DEBABD/image-size/small?v=v2&amp;amp;px=200" role="button" title="2.png" alt="2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My apologies for missing the tables. The first code creates the raw data set and the second one is the outcomes I intend. Here I attach the log.&lt;/P&gt;&lt;P&gt;The first code.&lt;/P&gt;&lt;PRE&gt;1    data RAW;
2    format FIRM $8. DATE yymmddn8.;
3    do FIRM="GOOGL","MSFT";
4    do DATE="12apr2019"d to "22apr2019"d;
5    RETURN=round(0.01+sqrt(0.01)*rannor(1),0.01);
6    output;
7    end;
8    end;
9    run;

NOTE: The data set WORK.RAW has 22 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.01 seconds&lt;/PRE&gt;&lt;P&gt;The second code.&lt;/P&gt;&lt;PRE&gt;12   data PROCESSED;
13   set RAW;
14   by FIRM DATE;
15
16   /*1. Conventional. Works nicely, but too many lines*/
17   if first.FIRM then LOG=log(1+RETURN);
18   else LOG+log(1+RETURN);
19   CUMULATIVE=exp(LOG)-1;
20
21   /*2. Tried IFN to shorten, but doesn't work*/
22   LOG1=ifn(first.FIRM,log(1+RETURN),LOG1+log(1+RETURN));
23
24   /*3. Wondered if it's about LAG, but wasn't*/
25   LOG2=ifn(first.FIRM,log(1+RETURN),lag(LOG2)+log(1+RETURN));
26
27   /*4. Tried to accumulate by multiplying, but doesn't work*/
28   if first.FIRM then CUMULATIVE1=RETURN;
29   else CUMULATIVE1=(1+CUMULATIVE1)*(1+RETURN)-1;
30
31   /*5. Wondered if it's about LAG, but wasn't*/
32   if first.FIRM then CUMULATIVE2=RETURN;
33   else CUMULATIVE2=(1+lag(CUMULATIVE2))*(1+RETURN)-1;
34
35   /*6. Tried to shorten using IFN, but doesn't work*/
36   CUMULATIVE3=ifn(first.FIRM,RETURN,(1+CUMULATIVE3)*(1+RETURN)-1);
37
38   /*7. Tried the same thing with a LAG function, but doesn't work*/
39   CUMULATIVE4=ifn(first.FIRM,RETURN,(1+lag(CUMULATIVE4))*(1+RETURN)-1);
40   run;

NOTE: Missing values were generated as a result of performing an operation on missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      22 at 22:39   22 at 25:44   20 at 29:20   20 at 33:20   22 at 36:37   22 at 36:50   22 at 36:61   22 at 39:37
      22 at 39:55   22 at 39:66
NOTE: There were 22 observations read from the data set WORK.RAW.
NOTE: The data set WORK.PROCESSED has 22 observations and 11 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds&lt;/PRE&gt;&lt;P&gt;Sorry again.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Apr 2019 21:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplicative-Acumulation/m-p/553054#M153762</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2019-04-22T21:03:36Z</dc:date>
    </item>
  </channel>
</rss>

