<?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 rate of change bw last and first vars by category in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201938#M4460</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You were very close. The first. and last. variables indicate that this record is the first or last for the group, it doesn't contain the variable value. You need to keep the first value around, and then use your formula on the last record.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I added a sort, as this will return incorrect data if your input file isn't sorted correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=ind_sect;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by sector date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data ind_per;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by sector;&lt;/P&gt;&lt;P&gt;retain FirstPrice;&lt;/P&gt;&lt;P&gt;if first.sector&lt;/P&gt;&lt;P&gt;then FirstPrice = price;&lt;/P&gt;&lt;P&gt;if last.sector&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; return = (price - FirstPrice)/FirstPrice;&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 15 Mar 2015 17:38:52 GMT</pubDate>
    <dc:creator>TomKari</dc:creator>
    <dc:date>2015-03-15T17:38:52Z</dc:date>
    <item>
      <title>calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201937#M4459</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;suppose I have the following data:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="1" class="jiveBorder" height="174" style="border: 1px solid rgb(0, 0, 0); width: 339px; height: 158px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;date&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;sector&lt;/STRONG&gt;&lt;/TH&gt;&lt;TH style="text-align: center; background-color: #6690bc; color: #ffffff; padding: 2px;" valign="middle"&gt;&lt;STRONG&gt;price&lt;/STRONG&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Jan2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Feb2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Mar2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;1&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Jun2009&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Oct2010&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="padding: 2px;"&gt;Jan2011&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;2&lt;/TD&gt;&lt;TD style="padding: 2px;"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;what I would like to get is the rate of return bw the last obs and the first obs for each sector. For ex, for sector 1 its = (6-12)/6 and for sector 2 its (10-8)/8.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I incorporated the following lines in my code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data ind_per;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;return = (last.price - first.price)/first.price ;&lt;/P&gt;&lt;P&gt;by sector;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;but got lots of errors and warning messages and my result wasn't what I wanted.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 17:29:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201937#M4459</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-15T17:29:05Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201938#M4460</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You were very close. The first. and last. variables indicate that this record is the first or last for the group, it doesn't contain the variable value. You need to keep the first value around, and then use your formula on the last record.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I added a sort, as this will return incorrect data if your input file isn't sorted correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tom&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=ind_sect;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by sector date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data ind_per;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by sector;&lt;/P&gt;&lt;P&gt;retain FirstPrice;&lt;/P&gt;&lt;P&gt;if first.sector&lt;/P&gt;&lt;P&gt;then FirstPrice = price;&lt;/P&gt;&lt;P&gt;if last.sector&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; return = (price - FirstPrice)/FirstPrice;&lt;/P&gt;&lt;P&gt;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 17:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201938#M4460</guid>
      <dc:creator>TomKari</dc:creator>
      <dc:date>2015-03-15T17:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201939#M4461</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks a lot for the code it worked perfectly!!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;On the same note, may I ask you a small variation of the question:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also wanted to find the return for each period and did the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data ind_lag;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;by sector;&lt;/P&gt;&lt;P&gt;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;The only problem is that it doesn't differentiate bw the sectors and therefore calculates the return of the first period of a sector using the last period of the previous sector, whereas the first period of each sector should be empty&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 20:53:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201939#M4461</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-15T20:53:55Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201940#M4462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Try:&lt;/P&gt;&lt;P&gt;data ind_lag;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;by sector;&lt;/P&gt;&lt;P&gt;if not first.sector then 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;or&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data ind_lag;&lt;/P&gt;&lt;P&gt;set ind_sect;&lt;/P&gt;&lt;P&gt;by sector;&lt;/P&gt;&lt;P&gt;k=lag(price);&lt;/P&gt;&lt;P&gt;if not first.sector then return = (price - k) / k;&lt;/P&gt;&lt;P&gt;drop k;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 21:02:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201940#M4462</guid>
      <dc:creator>naveen_srini</dc:creator>
      <dc:date>2015-03-15T21:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201941#M4463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi naveen, thanks for the code, it seems very intuitive but when I ran it I got the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE cellpadding="5" cellspacing="0" class="table" frame="box" rules="all" summary="Procedure Print: Data Set WORK.IND_LAG"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;1&lt;/TH&gt;&lt;TD class="r data"&gt;JAN2007&lt;/TD&gt;&lt;TD class="l data"&gt; &lt;/TD&gt;&lt;TD class="r data"&gt;0&lt;/TD&gt;&lt;TD class="r data"&gt;6698&lt;/TD&gt;&lt;TD class="r data"&gt;43.58231&lt;/TD&gt;&lt;TD class="r data"&gt;. &lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;2&lt;/TH&gt;&lt;TD class="r data"&gt;FEB2007&lt;/TD&gt;&lt;TD class="l data"&gt; &lt;/TD&gt;&lt;TD class="r data"&gt;0&lt;/TD&gt;&lt;TD class="r data"&gt;6699&lt;/TD&gt;&lt;TD class="r data"&gt;43.14695&lt;/TD&gt;&lt;TD class="r data"&gt;. &lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;3&lt;/TH&gt;&lt;TD class="r data"&gt;MAR2007&lt;/TD&gt;&lt;TD class="l data"&gt; &lt;/TD&gt;&lt;TD class="r data"&gt;0&lt;/TD&gt;&lt;TD class="r data"&gt;6703&lt;/TD&gt;&lt;TD class="r data"&gt;43.43580&lt;/TD&gt;&lt;TD class="r data"&gt;0.00669&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;for my sector 0 it omits the first 2 periods instead of the first one period, and for the following periods I get something like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE cellpadding="5" cellspacing="0" class="table" frame="box" rules="all" summary="Procedure Print: Data Set WORK.IND_LAG"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;96&lt;/TH&gt;&lt;TD class="r data"&gt;DEC2014&lt;/TD&gt;&lt;TD class="l data"&gt; &lt;/TD&gt;&lt;TD class="r data"&gt;0&lt;/TD&gt;&lt;TD class="r data"&gt;7008&lt;/TD&gt;&lt;TD class="r data"&gt;65.51498&lt;/TD&gt;&lt;TD class="r data"&gt;0.02895&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;97&lt;/TH&gt;&lt;TD class="r data"&gt;JAN2007&lt;/TD&gt;&lt;TD class="l data"&gt;1&lt;/TD&gt;&lt;TD class="r data"&gt;1&lt;/TD&gt;&lt;TD class="r data"&gt;15&lt;/TD&gt;&lt;TD class="r data"&gt;24.19350&lt;/TD&gt;&lt;TD class="r data"&gt;. &lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TH class="r rowheader" scope="row"&gt;98&lt;/TH&gt;&lt;TD class="r data"&gt;FEB2007&lt;/TD&gt;&lt;TD class="l data"&gt;1&lt;/TD&gt;&lt;TD class="r data"&gt;1&lt;/TD&gt;&lt;TD class="r data"&gt;14&lt;/TD&gt;&lt;TD class="r data"&gt;25.17784&lt;/TD&gt;&lt;TD class="r data" nowrap="nowrap"&gt;-0.61569&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;JAN2007 for sector 1 is omitted as it should be, but the return for FEB2007 uses the period DEC2014 instead of JAN2007&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 21:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201941#M4463</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-15T21:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201942#M4464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi again naveen, &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I did the second code that you put and it worked!!!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sorry didn't see it at first, for some reason it didn't show on my email message&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Mar 2015 21:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201942#M4464</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-15T21:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: calculating rate of change bw last and first vars by category</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201943#M4465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Tom,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks again for the code, its what I wanted!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was just wandering, is it possible to turn your code into a macro where I actually input the first and last periods (i.e, the starting and ending dates)?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thnaks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Mar 2015 02:26:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/calculating-rate-of-change-bw-last-and-first-vars-by-category/m-p/201943#M4465</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2015-03-17T02:26:27Z</dc:date>
    </item>
  </channel>
</rss>

