<?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: Finding the last observation in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303677#M8617</link>
    <description>&lt;P&gt;Or sort the other direction...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by id descending amount;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;retain lastVal;&lt;/P&gt;
&lt;P&gt;if first.id then lastVal=amount;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=want;&lt;/P&gt;
&lt;P&gt;by id amount;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Oct 2016 22:31:17 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-10T22:31:17Z</dc:date>
    <item>
      <title>Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303604#M8611</link>
      <description>&lt;P&gt;I am wanting to have SAS return the last weight for each ID. I was able to do this successfully with the intial weight but the final weight is pulling the last weight from the previous ID. Why is this happening?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Test1; set Test;&lt;BR /&gt;by ID;&lt;BR /&gt;retain last_avrgweight;&lt;BR /&gt;if last.ID then last_avrgweight=AvrgWeight;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE cellspacing="0" cellpadding="0" border="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Weight&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;103&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;105&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;12&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;106&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;210&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;211&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;212&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;215&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;25&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;216&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 10 Oct 2016 16:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303604#M8611</guid>
      <dc:creator>llt34c</dc:creator>
      <dc:date>2016-10-10T16:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303610#M8612</link>
      <description>&lt;P&gt;Your question is unclear. How is it not working? What are you trying to get vs what do you have?&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 17:09:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303610#M8612</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-10T17:09:41Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303611#M8613</link>
      <description>&lt;P&gt;I want SAS to take the last weight for each ID and list that as a new variable. Currently, SAS is taking the last weight from the previous ID and listing it as the new variable (thus this is the incorrect ID).&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 17:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303611#M8613</guid>
      <dc:creator>llt34c</dc:creator>
      <dc:date>2016-10-10T17:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303614#M8615</link>
      <description>&lt;P&gt;You have specified retain and only store it at the last record so that's correct behaviour.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It would help if you illustrated what you wanted but right now my suggestion would be to remove retain.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 17:21:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303614#M8615</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-10T17:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303619#M8616</link>
      <description>&lt;P&gt;Essentially, you are asking SAS to take the last value found, and then go back and add that to earlier observations.&amp;nbsp; That can be done, but requires a more complex DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;last_avrgweight=AvrgWeight;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The top DO loop reads all the observations for an ID, so the last value is available once that loop ends.&amp;nbsp; The bottom loop reads the same observations, and outputs them (including the value for the new variable).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 17:35:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303619#M8616</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-10T17:35:28Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303677#M8617</link>
      <description>&lt;P&gt;Or sort the other direction...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=have;&lt;/P&gt;
&lt;P&gt;by id descending amount;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by id;&lt;/P&gt;
&lt;P&gt;retain lastVal;&lt;/P&gt;
&lt;P&gt;if first.id then lastVal=amount;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=want;&lt;/P&gt;
&lt;P&gt;by id amount;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 22:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303677#M8617</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-10T22:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303686#M8618</link>
      <description>&lt;P&gt;That works as long as the last value is also the largest value. &amp;nbsp;That might be true here, and the sample data shows it that way. &amp;nbsp;But I didn't want to assume it if it wasn't stated.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 23:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303686#M8618</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-10T23:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the last observation</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303776#M8622</link>
      <description>Thank you! I really appreciate it!&lt;BR /&gt;</description>
      <pubDate>Tue, 11 Oct 2016 13:32:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Finding-the-last-observation/m-p/303776#M8622</guid>
      <dc:creator>llt34c</dc:creator>
      <dc:date>2016-10-11T13:32:29Z</dc:date>
    </item>
  </channel>
</rss>

