<?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: Stock return and do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284905#M58145</link>
    <description>&lt;P&gt;The automatic variable _n_ has the row number and can be used for such processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thesis.test7;
   set thesis.test2;
   array returns SPY--AFL;
   if _n_ &amp;gt; &amp;amp;k then do i= 1 to dim(returns);
   returns[i]=(returns[i]-lag(returns[i]))/lag(return​s[i]);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;HOWEVER&lt;/STRONG&gt;, the lag function, when it executes for row 7 all the lagged values&amp;nbsp;will be&amp;nbsp;missing because that is the first time the lag function is called.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might be better off creating an array of &amp;nbsp;non-conditional lagged values and reference that array instead of using lag(returns[i])&lt;/P&gt;</description>
    <pubDate>Fri, 15 Jul 2016 17:49:08 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-07-15T17:49:08Z</dc:date>
    <item>
      <title>Stock return and do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284833#M58138</link>
      <description>&lt;P&gt;Hi I have this code&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%let K=6;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data thesis.test7;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set thesis.test2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;array returns SPY--AFL;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;do i= 1+&amp;amp;K to dim(returns);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;returns[i]=(returns[i]-lag(returns[i]))/lag(returns[i]);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I would like the return computation to start at row 1+K and have the previous rows blank but it starts at column 1+K with the stock prices shown for previous columns.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What am I doing wrong?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks in advance.&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12755i8ABAF87835DF83F7/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Screen Shot 2016-07-15 at 16.06.13.png" title="Screen Shot 2016-07-15 at 16.06.13.png" /&gt;</description>
      <pubDate>Fri, 15 Jul 2016 14:12:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284833#M58138</guid>
      <dc:creator>Anon123</dc:creator>
      <dc:date>2016-07-15T14:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Stock return and do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284905#M58145</link>
      <description>&lt;P&gt;The automatic variable _n_ has the row number and can be used for such processing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thesis.test7;
   set thesis.test2;
   array returns SPY--AFL;
   if _n_ &amp;gt; &amp;amp;k then do i= 1 to dim(returns);
   returns[i]=(returns[i]-lag(returns[i]))/lag(return​s[i]);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;HOWEVER&lt;/STRONG&gt;, the lag function, when it executes for row 7 all the lagged values&amp;nbsp;will be&amp;nbsp;missing because that is the first time the lag function is called.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might be better off creating an array of &amp;nbsp;non-conditional lagged values and reference that array instead of using lag(returns[i])&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2016 17:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284905#M58145</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-15T17:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: Stock return and do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284946#M58164</link>
      <description>Cool thanks I'll try that out. Just another quick question, I'm new to the language SAS uses,&lt;BR /&gt;is there a way to generalize&lt;BR /&gt;array returns SPY--AFL&lt;BR /&gt;i.e. take the first column to the last column?&lt;BR /&gt;&lt;BR /&gt;Thank you for your time.</description>
      <pubDate>Fri, 15 Jul 2016 19:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284946#M58164</guid>
      <dc:creator>Anon123</dc:creator>
      <dc:date>2016-07-15T19:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Stock return and do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284957#M58170</link>
      <description>&lt;P&gt;To you mean to reverse the order of processing (AFL first and SPY last)? Easiest would be to change the index of the do loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do i = dim(returns) to 1 by (-1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are thinking of something else you may need to go into more details and the reasons why. Preferrably in a separate topic.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2016 19:58:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284957#M58170</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-15T19:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: Stock return and do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284997#M58178</link>
      <description>&lt;PRE&gt;
Better post your data as text , No one would like to type it for you .
And most important thing is post your output too .

&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Jul 2016 06:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stock-return-and-do-loop/m-p/284997#M58178</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-16T06:10:40Z</dc:date>
    </item>
  </channel>
</rss>

