<?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: Calculating rolling RSI for each stock by using the proc expand function? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866442#M342169</link>
    <description>I will take a look at the docu for the expand function,thank you</description>
    <pubDate>Mon, 27 Mar 2023 00:46:11 GMT</pubDate>
    <dc:creator>gotchj1</dc:creator>
    <dc:date>2023-03-27T00:46:11Z</dc:date>
    <item>
      <title>Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866253#M342095</link>
      <description>&lt;P&gt;I am trying to calculate the technical indicator relative strength index for each stock (Permno); I found a code online that does this but also includes a macro. I saw a forum here that mentions using a proc expand function instead of the macro that loops from day 16 to day 7500 for each stock. However, I am unfamiliar with proc expand. I have provided the csv file i am working with as well as the code I am using. I will also provide the link to the website that shows the RSI calculation using the macro.&lt;/P&gt;&lt;P&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings12/163-2012.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings12/163-2012.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; RSI2;&lt;/P&gt;&lt;P&gt;set RSI;&lt;/P&gt;&lt;P&gt;by permno;&lt;/P&gt;&lt;P&gt;ladjprc = lag(QQQQ_Close);&lt;/P&gt;&lt;P&gt;if QQQQ_Close&amp;gt;ladjprc then gain1 = QQQQ_Close-ladjprc;&lt;/P&gt;&lt;P&gt;if QQQQ_Close&amp;lt;ladjprc then loss1 = ladjprc-QQQQ_Close;&lt;/P&gt;&lt;P&gt;if first.Permno then do;&lt;/P&gt;&lt;P&gt;gain1 = &lt;STRONG&gt;.&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;loss1 = &lt;STRONG&gt;.&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;retain days &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;day +&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;if first.permno then day = &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;if gain1 = &lt;STRONG&gt;.&lt;/STRONG&gt; then gain1 = &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;if loss1 = &lt;STRONG&gt;.&lt;/STRONG&gt; then loss1 = &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;retain sumgain &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;sumgain = sumgain + gain1;&lt;/P&gt;&lt;P&gt;retain sumloss &lt;STRONG&gt;0&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;sumloss = sumloss + loss1;&lt;/P&gt;&lt;P&gt;if day = &lt;STRONG&gt;15&lt;/STRONG&gt; then do;&lt;/P&gt;&lt;P&gt;AveGain = (sumgain-gain1) / &lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;AveLoss = (sumloss-loss1) / &lt;STRONG&gt;14&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;RS = AveGain / AveLoss;&lt;/P&gt;&lt;P&gt;RSI = &lt;STRONG&gt;100&lt;/STRONG&gt;-&lt;STRONG&gt;100&lt;/STRONG&gt;/(&lt;STRONG&gt;1&lt;/STRONG&gt;+RS);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Mar 2023 09:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866253#M342095</guid>
      <dc:creator>gotchj1</dc:creator>
      <dc:date>2023-03-25T09:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866321#M342115</link>
      <description>&lt;P&gt;From the docu &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/etsug/etsug_expand_overview.htm" target="_self"&gt;here&lt;/A&gt;:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;The EXPAND procedure converts time series from one sampling interval or frequency to another and interpolates missing values in time series.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc Expand helps you to prepare your time series data for analysis. The procedure supports many different algorithms for interpolation and you would need to tell us what you want to use.&lt;/P&gt;
&lt;P&gt;The code logic required for a moving average gets of course simpler if the time series data doesn't contain missing data points.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2023 02:09:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866321#M342115</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-26T02:09:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866441#M342168</link>
      <description>The first RSI (on day 15) is calculated differnently than the RSI's that come afterward. But i need to use the previous Average Gain/Loss in each calculation when day is greater than 15. Could i add in an else statement that retains the previous Average gain or loss then recalculates the RSI. The new Average gain/loss for days greater than 15 is ((previous average gain/loss *13) + current gain/loss)/14</description>
      <pubDate>Mon, 27 Mar 2023 00:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866441#M342168</guid>
      <dc:creator>gotchj1</dc:creator>
      <dc:date>2023-03-27T00:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866442#M342169</link>
      <description>I will take a look at the docu for the expand function,thank you</description>
      <pubDate>Mon, 27 Mar 2023 00:46:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866442#M342169</guid>
      <dc:creator>gotchj1</dc:creator>
      <dc:date>2023-03-27T00:46:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866511#M342212</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I recall right then it was you who provided some months ago as answer to another question around rolling "something" the generic data step logic how that's done.&amp;nbsp;&lt;BR /&gt;Unfortunately I can't find this answer anymore but I believe it would also clarify a few things for the OP here.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2023 11:35:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866511#M342212</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-03-27T11:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866667#M342276</link>
      <description>I was thinking that I don't need to do the proc expand function and was going to use the retain function. The average Gian and average loss start on day 14 and then are calculated differently on day 15 and later. I was trying this code but it doesn't seem to work properly. I just need to be able to start the retain from day 15.&lt;BR /&gt;&lt;BR /&gt;data RSI2;&lt;BR /&gt;set RSI;&lt;BR /&gt;by permno days;&lt;BR /&gt;retain AveGain_new;&lt;BR /&gt;if days ge 16 then AveGain_new = AveGain;&lt;BR /&gt;AveGain_new=(AveGain_new *13 +gain)/14;&lt;BR /&gt;run;</description>
      <pubDate>Tue, 28 Mar 2023 04:09:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866667#M342276</guid>
      <dc:creator>gotchj1</dc:creator>
      <dc:date>2023-03-28T04:09:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating rolling RSI for each stock by using the proc expand function?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866843#M342342</link>
      <description>&lt;P&gt;This should work for you. It calculates the mean gains and losses using a simple average for the first 14 daily changes, and Wilder's method for the remaining means. Hopefully, the comments in the code clearly explains the inner workings, but feel free to ask.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data rsi;
set have;
by stock;

* compute daily change in price;
prev_close = lag(close);
change = close - prev_close;

* determine gain or loss;
if change &amp;gt; 0 then gain = change;
else if change &amp;lt; 0 then loss = abs(change);

* reset values when stock changes;
if first.stock then do;
    days = 0;
    prev_close = .;
    change = .;
    gain = .;
    loss = .;
    sum_gain = .;
    sum_loss = .;
    mean_gain = .;
    mean_loss = .;
end;

days + 1;

* save the calculated averages across iterations of data step;
retain mean_gain mean_loss;

* aggregate the daily gains and losses for the first 15 days (but only has 14 days of daily changes);
if days &amp;lt;= 15 then do;
    sum_gain + gain;
    sum_loss + loss;
    if days = 15 then do;
        * simple average of first 14 values for 1st average;
        mean_gain = sum_gain / 14;
        mean_loss = sum_loss / 14;
    end;
end;
else do;
    * Wilder's smoothing method for remaining averages &amp;gt; day 15;
    * the mean_ values on the right side of equation are prior means;
    mean_gain = (13 * mean_gain + coalesce(gain, 0)) / 14;
    mean_loss = (13 * mean_loss + coalesce(loss, 0)) / 14;
end;

* calculate RSI beginning at day 15 because
* 14 days of daily changes exist at this point;
if days &amp;gt;= 15 then do;
    rs = mean_gain / mean_loss;
    rsi = 100 - 100 / (1 + rs);
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I used the stock prices in sashelp.stocks to create the test data. Here is the code for that.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sort data=sashelp.stocks out=stocks;
where stock in ('IBM','Intel');
by stock date;
run;

* keep only 30 days of closing prices for each stock;
data have;
set stocks;
by stock;
retain days;
if first.stock then days = 0;
days + 1;
if days &amp;lt;= 30 then output;
keep stock date close;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Mar 2023 19:11:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-rolling-RSI-for-each-stock-by-using-the-proc-expand/m-p/866843#M342342</guid>
      <dc:creator>FloydNevseta</dc:creator>
      <dc:date>2023-03-28T19:11:27Z</dc:date>
    </item>
  </channel>
</rss>

