<?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 How to express to the power of 1/11 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508532#M136579</link>
    <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to express to the power of 1/11? I tried a lot of ways but didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ret12_2m = ((1+ret12)*(1+ret11)*(1+ret10)*(1+ret9)*(1+ret8)*(1+ret7)*(1+ret6)*(1+ret5)*(1+ret4)*(1+ret3)*(1+ret2))**(1/11)-1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
    <pubDate>Mon, 29 Oct 2018 22:27:21 GMT</pubDate>
    <dc:creator>Songchan</dc:creator>
    <dc:date>2018-10-29T22:27:21Z</dc:date>
    <item>
      <title>How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508532#M136579</link>
      <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you know how to express to the power of 1/11? I tried a lot of ways but didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ret12_2m = ((1+ret12)*(1+ret11)*(1+ret10)*(1+ret9)*(1+ret8)*(1+ret7)*(1+ret6)*(1+ret5)*(1+ret4)*(1+ret3)*(1+ret2))**(1/11)-1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 22:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508532#M136579</guid>
      <dc:creator>Songchan</dc:creator>
      <dc:date>2018-10-29T22:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508537#M136584</link>
      <description>&lt;P&gt;x**(1/11) is OK. What problem did you meet? Please post the SAS log.&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 22:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508537#M136584</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-10-29T22:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508539#M136586</link>
      <description>&lt;P&gt;Is it just your parentheses?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ret12_2m = (((1+ret12)*(1+ret11)*(1+ret10)*(1+ret9)*(1+ret8)*(1+ret7)*(1+ret6)*(1+ret5)*(1+ret4)*(1+ret3)*(1+ret2))**(1/11))-1;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Oct 2018 22:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508539#M136586</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2018-10-29T22:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508595#M136610</link>
      <description>&lt;P&gt;Am I right that you want a constant single-period rate of return which compounds to an actual 11-period return?&amp;nbsp; Presumably your data are in a time series (likely 1 record per month).&amp;nbsp; If so, you don't have to constantly add or multiply 11 periods.&amp;nbsp; Instead maintain a variable, call it LOGPLUS1 which has the 11-period sum of log(1+ret).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, with each incoming record, add log(1+ret) of the period entering the time span, and subtract log(1+ret) of the period leaving the time span. Afterwards (1) divide by 11, (2) exponentiate, and (3) subtract 1 for the desired value.&amp;nbsp; This can be conveniently done with the lag function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say you want the 11-month return rate for 1 month back through 11 months back.&amp;nbsp; (i.e. for the December record, you want the monthly rate for Jan (11 month lag) through Nov (1 month lag).&amp;nbsp; Then for the subsequent January you want to take away the previous January (12 month lag) and add the immediately preceding December (1 month lag):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  retain logplus1 0;
  logplus1=sum(logplus1,lag1(log(1+ret)),-1*sum(0,lag12(log(1+ret))));
  if _n_&amp;gt;11 then x= exp(logplus1/11)-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason for&amp;nbsp;&amp;nbsp; "-1*sum(0,lag12(log12(1+ret)))"&amp;nbsp;&amp;nbsp; instead of&amp;nbsp;&amp;nbsp; "-lag12(log(1+ret))"&amp;nbsp; is to avoid errors due to attempting to subtract a missing value, which would generate a missing value for logplus1.&amp;nbsp; But "-sum(x,0)" will always generate a non-missing value, even if x is missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if your data is sorted by id and month, then you have to avoid contaminating one id with returns from the prior id.&amp;nbsp; So only add lag1 and subtract lag12 when the corresponding ID's match:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  by id;
  retain logplus1 0;
  if first.id then logplus1=0;
  logplus1=sum(logplus1
               ,ifn(id=lag1(id),lag1(log(1+ret)),0)
               ,ifn(id=lag12(id),-1*sum(0,lag12(log12(1+ret))),0)
               );
  if lag11(id)=id then x= exp(logplus1/11)-1;
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>Tue, 30 Oct 2018 14:03:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508595#M136610</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-30T14:03:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508670#M136645</link>
      <description>&lt;P&gt;It seems you want get a portfolio's expect return by its geometric mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Check function&amp;nbsp;GEOMEAN(argument&amp;lt;,argument,…&amp;gt;)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;x3=geomean(of x1-x2); &lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 13:03:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508670#M136645</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-10-30T13:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508837#M136694</link>
      <description>&lt;P&gt;Thank you for replying.&lt;/P&gt;&lt;P&gt;I&amp;nbsp;am trying&amp;nbsp;to calculate compounded returns of prior 12 to 2 month, so my whole procedure is like this,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp01; set mylib.crsp1969b; 
	ret2=lag2(ret); ret3=lag3(ret);
	ret4=lag4(ret); ret5=lag5(ret); ret6=lag6(ret);
	ret7=lag7(ret); ret8=lag8(ret); ret9=lag9(ret);
	ret10=lag10(ret); ret11=lag11(ret); ret12=lag12(ret);

	perm2=lag2(permno); perm3=lag3(permno);perm4=lag4(permno); 
    perm5=lag5(permno); perm6=lag6(permno);perm7=lag7(permno);
	perm8=lag8(permno); perm9=lag9(permno);perm10=lag10(permno);
	perm11=lag11(permno);perm12=lag12(permno);

	if permno^=perm2 then ret2=.;
	if permno^=perm3 then ret3=.;
	if permno^=perm4 then ret4=.;
	if permno^=perm5 then ret5=.;
	if permno^=perm6 then ret6=.;
    if permno^=perm7 then ret7=.;
	if permno^=perm8 then ret8=.;
	if permno^=perm9 then ret9=.;
	if permno^=perm10 then ret10=.;
	if permno^=perm11 then ret11=.;
	if permno^=perm12 then ret12=.;
	if permno=perm12 then 
		ret12_2m =((1+ret12)*(1+ret11)*(1+ret10)*(1+ret9)*(1+ret8)*(1+ret7)*
                   (1+ret6)*(1+ret5)*(1+ret4)*(1+ret3)*(1+ret2))**(1/11)-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 30 Oct 2018 17:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/508837#M136694</guid>
      <dc:creator>Songchan</dc:creator>
      <dc:date>2018-10-30T17:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to express to the power of 1/11</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/509249#M136883</link>
      <description>&lt;P&gt;OK, so you want rolling returns for an 11-month or 11-day period, starting 12 periods before the current period through 2 periods before the current period.&amp;nbsp; (I.e. for January 2018, just get the return for&amp;nbsp;January 2017&amp;nbsp;through November 2017).&amp;nbsp; Or the equivalent in trading days is you're using the CRSP daily data file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The suggestion below diminishes the amount of lag queues and multiplications.&amp;nbsp;&amp;nbsp; It maintains a variable, GEOMEAN2_12&amp;nbsp;which is the product of (1+ret12)*(1+ret11)...*(1+ret2) - except you will see I don't create variables ret2, ... ret12.&amp;nbsp; Note the RETAIN statement below tells SAS not to reset GEOMEAN2_12 to missing upon the start of a new observation.&amp;nbsp; It keeps the old value, which conveniently can be modify to produce the new value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When each new observation is encountered, simply take the preceding geomean2_12 and&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Divide by the oldest (1+ret), namely lag13(1+ret).&lt;/LI&gt;
&lt;LI&gt;Multiply by the new entry lag2(1+ret).&lt;/LI&gt;
&lt;LI&gt;Afterwards just make ret2_12 = &lt;EM&gt;&lt;STRONG&gt;geomean2_12-1&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;&amp;nbsp;except for the first 12 records where ret2_12 is left missing.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In other words, the concept (but see below) is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  retain geomean2_12 1;
  geomean2_12=  geomean2_12 *  (1+lag2(ret))/(1+lag13(ret));
  if lag12(id)=id then ret2_12 = geomean2_12-1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The problem here is the lag2(ret) and lag13(ret) start out as missing values, making geomean2_12 missing.&amp;nbsp; So even when you get beyond the first 12 observations geomean2_12 is missing,&amp;nbsp;because when it's on both the left and right side of the assignment statement, it stays missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Introduce the SUM function.&amp;nbsp; Instead of "1+lag2(ret)" which is missing whenever lag2(ret) is missing use SUM(1,lag2(ret)), which will treat lag2(ret) as a zero even when it is missing,&amp;nbsp; and SUM(1,lag2(ret)) will equal 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  retain geomean2_12 1;
  geomean2_12=  geomean2_12 *  sum(1,lag2(ret)) / sum(1,lag13(ret)) ;
  if lag12(id)=id then ret2_12 = geomean2_12-1;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This generates no missing values for geomean2_12, and correct values for ret2_12.&amp;nbsp; And even though the first 12 observations of geomean2_12 for one ID is contaminated by RETs from the prior ID, it doesn't matter since RET2_12 is left as missing until the 13th record of each ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when there are many ID's, there can be a precision problem for geomean2_12 in the later ID's.&amp;nbsp; You could&amp;nbsp; test this by running the above on your entire file.&amp;nbsp; And then just run it on the last ID in your file.&amp;nbsp; There will be numeric differences due to the fact that computers are digital and of finite precision,&amp;nbsp;meaning the product of some values not be precisely&amp;nbsp;represented in geo_mean2.&amp;nbsp; This can compound over a large file unless geomean2_12 if corrected at some point.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Solution:&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;reset geomean2_12 to 1 at the beginning of each ID, and&lt;/LI&gt;
&lt;LI&gt;also multiply by lag2(1+ret) and divide by lag13(1+ret) only when they are for the same ID.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The IFN function in the code below (it's like the excel if function) will do this:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  retain geomean2_12 1;
  if first.id then geomean2_12=1;
  geomean2_12=  geomean2_12 
               *sum(1,ifn(lag2(id)=id,lag2(ret),0))
               /sum(1,ifn(lag13(id)=id,lag13(ret),0)) ;
  if lag12(id)=id then ret2_12 = geomean2_12-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expression&amp;nbsp;&amp;nbsp; "IFN(lag2(id)=id,lag2(ret),0)" does the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Tests whether lag2(id) equals current ID&lt;/LI&gt;
&lt;LI&gt;If true then return the value of lag2(ret)&lt;/LI&gt;
&lt;LI&gt;Otherwise return a 0&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Same technique is used for the divisor "IFN(lag13(id)=id,lag13(ret),0)"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This avoids compounding numeric imprecision across multiple ID's&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you will find this faster and easier to maintain.&amp;nbsp; For instance if you want a different historic window size or window offset, you just have to change&amp;nbsp;the 2's and the 13's.&amp;nbsp; No statements to add or remove.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Oct 2018 18:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-express-to-the-power-of-1-11/m-p/509249#M136883</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-31T18:34:28Z</dc:date>
    </item>
  </channel>
</rss>

