<?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 Proc Expand to Sum up (*Corrected) previous (monthly) returns in SAS Forecasting and Econometrics</title>
    <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373018#M2469</link>
    <description>&lt;P&gt;Hi to all. First I am just begginer to SAS and to understand SAS complex macros and terms is very hard at this point. I am replicating a financal&amp;nbsp;Table 1 &amp;amp; Table2 of&amp;nbsp;[Avramov, Doron, and Tarun Chordia, 2006 “Asset Pricing Models and Financial Market Anomalies,”]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To measure momentum, they used monthly&amp;nbsp;accumulated returns of&amp;nbsp;(previous)&amp;nbsp;last 2nd &amp;amp; 3rd (Ret23), 4th to 6th (Ret46), and 7th to 12th (Ret712)&amp;nbsp;months. Data itself is panel data in which I have firms (permno) monthly data for several years along with returns, BM, size and turnover. I have attached "merge1" file which I am using for generating returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First I tried to use a simple macro code which take logs, then do sum, and convert back to exp. Everything seems fine with codes but my results have totally different values and trends ( it should be Ret712&amp;gt;Ret46&amp;gt;Ret23)&amp;nbsp;. here are the codes I used initially. By using both option 1 and 2 did'nt improve anything.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=merge1 ;	by permno descending date;	run;
%macro calcret;
	data merge2;
	set merge1;
	by permno;
	lret=log(ret+1);	* Take natural logs of the firm return (ret);
	array lagret[12] lagret1-lagret12;
	%do j=1 %to 12;		* Macro "do-loop" to create the 12 lags;
	lagret&amp;amp;j=lag&amp;amp;j(lret);
	%end;

* Calculate the cumulative returns by adding the lags of the returns;
* Option 1;
	ret23=	(exp( sum(of lret lagret2-lagret3)) -1)*100;
	ret46=	(exp( sum(of lret lagret4-lagret6)) -1)*100;
	ret712=	(exp( sum(of lret lagret7-lagret12)) -1)*100;
*Option 2;
	sret23=	((exp( sum(lagret2,lagret3)))-1)*100;
	sret46=	((exp( sum(lagret4,lagret5,lagret6))) -1)*100;
	sret712=((exp( sum(lagret7,lagret8,lagret9,lagret10,lagret11,lagret12)))-1)*100;
	drop l: count i rf;	* Drop unnecessary variables;
%mend calcret;			* End of macro;
%calcret;			* Run the macro;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now after many attempts and searches, I am planning to try doing the same thing with PROC EXPAND. But I have two issues.&lt;/P&gt;&lt;P&gt;1:&amp;nbsp;Proc Expand&amp;nbsp;backward moving Average/Sum&amp;nbsp;include current and previous values. Is there anyway to accumulate/add/sum only previous values, i.e. starting from t-2 to t-3 (not t to t-3) and accumulated return from t-4 to t-6 (not t to t-6)..&lt;/P&gt;&lt;P&gt;2: Also is it ok to just add (sum up) the return to get accumulated return (which I doubt, bcoz returns are either&amp;nbsp;should be multiplied or converted to natural log before adding. Please give some suggestion)&lt;/P&gt;&lt;P&gt;3: In Proc Expand Chapter on SAS guide, there is this function which seems to do similar thing, as I did above&amp;nbsp;in macro codes.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=a out=b from=month to=week;
   id date;
   convert sales / observed=total
                   transformin=(log)
                   transformout=(exp);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can this code be modified to get summ of&amp;nbsp; required return by taking (Log) of IN values and then transforming OUT values as (Exp) function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lastly, I am not sticking to any particular approach. If you have any other way to calculate accumulated returns, please advise me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried best to explain everything is details. If I missed anything, plz feel free to ask.&lt;/P&gt;&lt;P&gt;Once again thanks alot in advance.&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jul 2017 09:28:21 GMT</pubDate>
    <dc:creator>omer2020</dc:creator>
    <dc:date>2017-07-07T09:28:21Z</dc:date>
    <item>
      <title>Proc Expand to Sum up (*Corrected) previous (monthly) returns</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373018#M2469</link>
      <description>&lt;P&gt;Hi to all. First I am just begginer to SAS and to understand SAS complex macros and terms is very hard at this point. I am replicating a financal&amp;nbsp;Table 1 &amp;amp; Table2 of&amp;nbsp;[Avramov, Doron, and Tarun Chordia, 2006 “Asset Pricing Models and Financial Market Anomalies,”]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To measure momentum, they used monthly&amp;nbsp;accumulated returns of&amp;nbsp;(previous)&amp;nbsp;last 2nd &amp;amp; 3rd (Ret23), 4th to 6th (Ret46), and 7th to 12th (Ret712)&amp;nbsp;months. Data itself is panel data in which I have firms (permno) monthly data for several years along with returns, BM, size and turnover. I have attached "merge1" file which I am using for generating returns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First I tried to use a simple macro code which take logs, then do sum, and convert back to exp. Everything seems fine with codes but my results have totally different values and trends ( it should be Ret712&amp;gt;Ret46&amp;gt;Ret23)&amp;nbsp;. here are the codes I used initially. By using both option 1 and 2 did'nt improve anything.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=merge1 ;	by permno descending date;	run;
%macro calcret;
	data merge2;
	set merge1;
	by permno;
	lret=log(ret+1);	* Take natural logs of the firm return (ret);
	array lagret[12] lagret1-lagret12;
	%do j=1 %to 12;		* Macro "do-loop" to create the 12 lags;
	lagret&amp;amp;j=lag&amp;amp;j(lret);
	%end;

* Calculate the cumulative returns by adding the lags of the returns;
* Option 1;
	ret23=	(exp( sum(of lret lagret2-lagret3)) -1)*100;
	ret46=	(exp( sum(of lret lagret4-lagret6)) -1)*100;
	ret712=	(exp( sum(of lret lagret7-lagret12)) -1)*100;
*Option 2;
	sret23=	((exp( sum(lagret2,lagret3)))-1)*100;
	sret46=	((exp( sum(lagret4,lagret5,lagret6))) -1)*100;
	sret712=((exp( sum(lagret7,lagret8,lagret9,lagret10,lagret11,lagret12)))-1)*100;
	drop l: count i rf;	* Drop unnecessary variables;
%mend calcret;			* End of macro;
%calcret;			* Run the macro;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Now after many attempts and searches, I am planning to try doing the same thing with PROC EXPAND. But I have two issues.&lt;/P&gt;&lt;P&gt;1:&amp;nbsp;Proc Expand&amp;nbsp;backward moving Average/Sum&amp;nbsp;include current and previous values. Is there anyway to accumulate/add/sum only previous values, i.e. starting from t-2 to t-3 (not t to t-3) and accumulated return from t-4 to t-6 (not t to t-6)..&lt;/P&gt;&lt;P&gt;2: Also is it ok to just add (sum up) the return to get accumulated return (which I doubt, bcoz returns are either&amp;nbsp;should be multiplied or converted to natural log before adding. Please give some suggestion)&lt;/P&gt;&lt;P&gt;3: In Proc Expand Chapter on SAS guide, there is this function which seems to do similar thing, as I did above&amp;nbsp;in macro codes.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=a out=b from=month to=week;
   id date;
   convert sales / observed=total
                   transformin=(log)
                   transformout=(exp);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Can this code be modified to get summ of&amp;nbsp; required return by taking (Log) of IN values and then transforming OUT values as (Exp) function?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lastly, I am not sticking to any particular approach. If you have any other way to calculate accumulated returns, please advise me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried best to explain everything is details. If I missed anything, plz feel free to ask.&lt;/P&gt;&lt;P&gt;Once again thanks alot in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jul 2017 09:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373018#M2469</guid>
      <dc:creator>omer2020</dc:creator>
      <dc:date>2017-07-07T09:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Expand to Accumulate previous (monthly) returns</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373025#M2470</link>
      <description>&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings10/093-2010.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings10/093-2010.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.lexjansen.com/wuss/2004/tutorials/i_tut_time_series_magic__usi.pdf" target="_blank"&gt;http://www.lexjansen.com/wuss/2004/tutorials/i_tut_time_series_magic__usi.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I strongly recommend reading the two papers above to use PROC EXPAND correctly and efficiently.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 14:45:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373025#M2470</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-04T14:45:57Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Expand to Accumulate previous (monthly) returns</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373150#M2471</link>
      <description>&lt;P&gt;thanks alot Reeza. Both articles were very useful and that trim funcion in first article&amp;nbsp;would save me alot.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But unluckily,&amp;nbsp;I could'nt find anything related to what I want to do. I still unable to find a way to having backward&amp;nbsp;moving Sum (MOVESUM) for previous 2nd &amp;amp; 3rd&amp;nbsp;months (excluding current) or for next step, moving average of previous 4th, 5th&amp;nbsp;and 6th month(excluding current and previous&amp;nbsp;1,2 &amp;amp; 3 months)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Permno Month Ret (Ret23) (Ret46)&lt;/P&gt;&lt;P&gt;10002 1 9&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&lt;/P&gt;&lt;P&gt;10002 2 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&lt;/P&gt;&lt;P&gt;10002 3 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (-)&lt;/P&gt;&lt;P&gt;10002 4 7&amp;nbsp;(sum of ret of&amp;nbsp;2 &amp;amp;&amp;nbsp;1 month) (-)&lt;/P&gt;&lt;P&gt;10002 5 8&amp;nbsp;(sum of ret of&amp;nbsp;3 &amp;amp; 2&amp;nbsp;month) (-)&lt;/P&gt;&lt;P&gt;10002 6 9&amp;nbsp;(sum of ret of&amp;nbsp;4 &amp;amp;&amp;nbsp;3 month) (-)&lt;/P&gt;&lt;P&gt;10002 7 9 (sum of ret of&amp;nbsp;5 &amp;amp;&amp;nbsp;4 month)&amp;nbsp;(sum ret of 3,&amp;nbsp;2 &amp;amp;&amp;nbsp;1 month)&lt;/P&gt;&lt;P&gt;10002 8 8 (sum of ret of&amp;nbsp;6 &amp;amp;&amp;nbsp;5 month)&amp;nbsp;(sum ret of 4,&amp;nbsp;3 &amp;amp;&amp;nbsp;2 month)&lt;/P&gt;&lt;P&gt;10002 9 11 (sum of ret of&amp;nbsp;7 &amp;amp;&amp;nbsp;6 month)&amp;nbsp;(sum ret of 5,&amp;nbsp;4 &amp;amp;&amp;nbsp;3 month)&lt;/P&gt;&lt;P&gt;10002 10 10 (sum of ret of&amp;nbsp;8 &amp;amp;&amp;nbsp;7 month)&amp;nbsp;(sum ret of 6,&amp;nbsp;5 &amp;amp;&amp;nbsp;4 month)&lt;/P&gt;&lt;P&gt;10002 11 9 (sum of ret of&amp;nbsp;9 &amp;amp;&amp;nbsp;8 month)&amp;nbsp;(sum ret of&amp;nbsp;7, 6&amp;nbsp;&amp;amp;&amp;nbsp;5 month)&lt;/P&gt;&lt;P&gt;10002 12 8 (sum of ret of 10 &amp;amp; 9 month)&amp;nbsp;(sum ret of 8, 7 &amp;amp; 6 month)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I am looking to achieve. values of (RET23/RET46) in brackets are what I want. the trim function can help me by inserting missing values if enough values are not availale. but how can i do this proc expand function to run moving Sum for specific numbers of months.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 05:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373150#M2471</guid>
      <dc:creator>omer2020</dc:creator>
      <dc:date>2017-07-05T05:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Expand to Accumulate previous (monthly) returns</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373260#M2475</link>
      <description>&lt;P&gt;SQL can do that .&lt;/P&gt;
&lt;P&gt;Post some data and output you want to see ,and let us better understand your question.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 13:18:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373260#M2475</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-07-05T13:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Expand to Accumulate previous (monthly) returns</title>
      <link>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373858#M2483</link>
      <description>&lt;P&gt;Hi Ksharp, Thank u for ur response. I have used the code given below to get Retun values for specific months, but I do that manually and use proc EXPAND only to generate lagged return values for the required (previous 2nd &amp;amp; 3rd, 4th 5th &amp;amp; 6th, 7th 8th 9th 10th 11th 12th months) periods.&lt;/P&gt;&lt;P&gt;But still I would love to know how it can be done by SQL. I am begginer to SAS and commited to learn more. I would really appriciate if you guide me to do the same in SQL.&lt;/P&gt;&lt;P&gt;I have tried to explain the data and what I want by giving an example above. I am attaching a small sample of my file so that you can understand it better.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CODES FOR ADDING RETUNS OF SPECIFIC PERIODS(MONTHS) BY PROC EXPAND TO GENERATE LAG &amp;amp; MANUAL ADDITION:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Generating Lagged values by Proc Expand;

proc expand data=merge1 out=lag  method = none;
by permno;
	convert ret = ret;
	convert ret = ret2 / transformout=(lag 2+100);
	convert ret = ret3 / transformout=(lag 3+100);
	convert ret = ret4 / transformout=(lag 4+100);
	convert ret = ret5 / transformout=(lag 5+100);	
	convert ret = ret6 / transformout=(lag 6+100);
	convert ret = ret7 / transformout=(lag 7+100);	
	convert ret = ret8 / transformout=(lag 8+100);
	convert ret = ret9 / transformout=(lag 9+100);
	convert ret = ret10 / transformout=(lag 10+100);
	convert ret = ret11 / transformout=(lag 11+100);
	convert ret = ret12 / transformout=(lag 12+100);

* All lagged generated variables are named as Returns;
 	*so I label them to avoid confusion and to have better;
	*visual understanding of data. it can be skipped;

	label ret2=lag2ret ret3=lag3ret ret4=lag4ret ret5=lag5ret
	ret6=lag6ret ret7=lag7ret ret8=lag8ret ret9=lag9ret ret10=lag10
	ret ret11=lag11ret ret12=lag12ret;
run;

*I removed firms with less than 12 months returns, proc Expand function;
*by using TRIM command can also do that but I am not very good in using it;

data lag2; set lag;
	if ret12 ne .;	
	run;

* My data require me to have at least 36 observation of returns and;
	*firm-level variable, so I use SQL in order to perform that;

proc sql;
	create table merge2 as select * from lag2
	group by permno having count(ret) ge 36
	and count(turnover) ge 36 and 	count(size) ge 36
	and count(bm) 	ge 36 and 	count(ret) ge 36;
	quit;

proc sort data=merge2;	by permno macc;	run;

* I add up returns, but in order to do that I need some extra steps;
* Normal way of adding returns is [ret1 &amp;amp; ret2 (in %);
	*&amp;gt; add 1 (which is 100%) &amp;gt; then multiply ret1 &amp;amp; ret2;
	*&amp;gt; then subtract 1 (which is again 100%) ];
* Since return values are in percent, but unlike excel,;
	*SAS don't treat these values as percent. So I add 100;
	*in PROC EXPAND while generating lagged values to;
	*avoid one extra step &amp;gt; Multiply Retuns values;
	*&amp;gt; Divide the product to 10 power 2n-2 &amp;gt; and then;
	*&amp;gt; subtract it with 100;

data mergexpnd;
set merge2;
ret23 = ((ret2*ret3)/10**2)-100;
ret46 = ((ret4*ret5*ret6)/10**4)-100;
ret712 = ((ret7*ret8*ret9*ret10*ret11*ret12)/10**10)-100;
run;

* Finally I droped lagged values of return after getting required results;

data mergexpnd;
set mergexpnd;
drop ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret10 ret11 ret12 time;
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>Fri, 07 Jul 2017 09:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Forecasting-and-Econometrics/Proc-Expand-to-Sum-up-Corrected-previous-monthly-returns/m-p/373858#M2483</guid>
      <dc:creator>omer2020</dc:creator>
      <dc:date>2017-07-07T09:24:53Z</dc:date>
    </item>
  </channel>
</rss>

