<?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: Outputting the first and last observation for each ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182899#M303276</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table one as&lt;BR /&gt;select * from have&lt;BR /&gt;order by id, date, value;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want (keep=id earliest last) ;&lt;/P&gt;&lt;P&gt;set one;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id and last.id then delete;&lt;BR /&gt;retain earliest;&lt;BR /&gt;if first.id then earliest=value;&lt;BR /&gt;if last.id then last=value;&lt;BR /&gt;if last.id;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 03 Jun 2014 15:56:34 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2014-06-03T15:56:34Z</dc:date>
    <item>
      <title>Outputting the first and last observation for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182896#M303273</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a dataset that has variables ID, Date, and Value. For each ID that has more than one Value, I want to output the earliest observation into a new column 'First', and the latest observation into a new column 'Last'. For IDs that only have one Value, I want the observation to be ignored. The final aim is to do a scatter plot of 'First' vs 'Last'.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help would be appreciated. &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt; Preferably using the UI in SAS EG, but if that isn't possible then a script will do as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jun 2014 12:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182896#M303273</guid>
      <dc:creator>misaochan</dc:creator>
      <dc:date>2014-06-03T12:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting the first and last observation for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182897#M303274</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First, you need to sort the dataset by ID and Date.&lt;/P&gt;&lt;P&gt;Then you do&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want (keep=ID first_date last_date first_value last_value);&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by ID;&lt;/P&gt;&lt;P&gt;retain first_date last_date first_value last_value counter;&lt;/P&gt;&lt;P&gt;if first.ID&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; counter = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; first_date = Date;&lt;/P&gt;&lt;P&gt;&amp;nbsp; first_value = Value;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;counter + 1;&lt;/P&gt;&lt;P&gt;if last.ID and counter &amp;gt; 1&lt;/P&gt;&lt;P&gt;then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_date = Date;&lt;/P&gt;&lt;P&gt;&amp;nbsp; last_value = Value;&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>Tue, 03 Jun 2014 12:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182897#M303274</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2014-06-03T12:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting the first and last observation for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182898#M303275</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table first as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select * from have group by id having date=min(date) and count(*)&amp;gt;1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table last as&lt;/P&gt;&lt;P&gt;&amp;nbsp; select * from have group by id having date=max(date) and count(*)&amp;gt;1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jun 2014 12:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182898#M303275</guid>
      <dc:creator>slchen</dc:creator>
      <dc:date>2014-06-03T12:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Outputting the first and last observation for each ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182899#M303276</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table one as&lt;BR /&gt;select * from have&lt;BR /&gt;order by id, date, value;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want (keep=id earliest last) ;&lt;/P&gt;&lt;P&gt;set one;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id and last.id then delete;&lt;BR /&gt;retain earliest;&lt;BR /&gt;if first.id then earliest=value;&lt;BR /&gt;if last.id then last=value;&lt;BR /&gt;if last.id;&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jun 2014 15:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Outputting-the-first-and-last-observation-for-each-ID/m-p/182899#M303276</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2014-06-03T15:56:34Z</dc:date>
    </item>
  </channel>
</rss>

