<?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: Arithmetic calculations that involved figures from different rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173976#M33407</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, I will try it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 17 Nov 2014 11:19:48 GMT</pubDate>
    <dc:creator>lior</dc:creator>
    <dc:date>2014-11-17T11:19:48Z</dc:date>
    <item>
      <title>Arithmetic calculations that involved figures from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173973#M33404</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hallow, does the SAS have the ability to make arithmetic calculations that involved figures from different rows?&lt;/P&gt;&lt;P&gt;For example if I have daily data on share prices and I want to calculate daily return of the shares, I need to take the share price,&amp;nbsp; subtract the share price at the former day&amp;nbsp;&amp;nbsp; (from the upper row) and dived it by the share price at the former day.&amp;nbsp; &lt;/P&gt;&lt;P&gt;This is very easy&amp;nbsp; to do in the excel and I wondered if the SAS able to do this kind of calculation as well. thanks, Lior&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="230" style="border: 1px solid rgb(0, 0, 0); width: 323px; height: 232px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Date&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Share Price&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;Share Daily Return&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;01/01/2012&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;02/01/2012&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;5&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;STRONG style="color: #ff00ff;"&gt;0.25 = ( 5 - 4 ) / 4&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;03/01/2012&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;&lt;STRONG style="color: #ff00ff;"&gt;1 = ( 10- 5 ) / 5&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;04/01/2012&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;8&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;&lt;STRONG style="color: #ff00ff;"&gt;-0.2 = ( 8 - 10 ) / 10&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Nov 2014 08:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173973#M33404</guid>
      <dc:creator>lior</dc:creator>
      <dc:date>2014-11-17T08:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic calculations that involved figures from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173974#M33405</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are several ways to do this. There is lag() function that can be used. Here, even that is not needed for the present data set. Just hold the current value as PREV and use it in calcuations.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data need;&lt;/P&gt;&lt;P&gt;retain prev;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if prev then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return = (Price - prev) / prev;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; prev = Price;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if _n_ = 1 then prev = Price;&lt;/P&gt;&lt;P&gt;drop prev;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Nov 2014 09:37:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173974#M33405</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2014-11-17T09:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic calculations that involved figures from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173975#M33406</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Lag() however would shrink your code somewhat, note also that both examples assume that all data is present, i.e. if you were missing 03/01/2012 then 04 would be compared to 02/01/2012.&lt;/P&gt;&lt;P&gt;Lag:&lt;/P&gt;&lt;P&gt;data need;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return=(price - lag(price)) /lag(price);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My suggestion however would be to explicitly merge the previous day on the current day to avoid missing data:&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; create table NEED as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.*,&lt;/P&gt;&lt;P&gt;&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; case&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when B.PRICE is not null then (A.PRICE - B.PRICE) / B.PRICE&lt;/P&gt;&lt;P&gt;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else . end as RETURN&amp;nbsp; /* Only calculate if previous record is present */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HAVE A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; left join&amp;nbsp;&amp;nbsp; HAVE B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A.DATE=(B.DATE + 1);&amp;nbsp;&amp;nbsp; /* Assumes date is numeric like date9. */&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Nov 2014 09:56:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173975#M33406</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-11-17T09:56:57Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic calculations that involved figures from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173976#M33407</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you, I will try it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Nov 2014 11:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173976#M33407</guid>
      <dc:creator>lior</dc:creator>
      <dc:date>2014-11-17T11:19:48Z</dc:date>
    </item>
    <item>
      <title>Re: Arithmetic calculations that involved figures from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173977#M33408</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In my research , if 03/01/2012 price data is missing (because for example its turn to be Saterday and no price data&amp;nbsp; been public), I &lt;SPAN style="text-decoration: underline;"&gt;would want&lt;/SPAN&gt; the 04/01/2012 to be compared to 02/01/2012 data&amp;nbsp; . so there no problem in that aspect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;Lior&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Nov 2014 11:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arithmetic-calculations-that-involved-figures-from-different/m-p/173977#M33408</guid>
      <dc:creator>lior</dc:creator>
      <dc:date>2014-11-17T11:24:55Z</dc:date>
    </item>
  </channel>
</rss>

