<?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: how can we get the first and last observations in a dataset using Proc SQl? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30003#M5694</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... if you do not have a record number on each observation and you'd rather not use unsupported monotonic(), how about ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select count(*) into :lastobs from have;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;create table want as&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select * from have (obs=1)&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;union&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select * from have (firstobs=&amp;amp;lastobs);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 14 Jan 2012 19:27:26 GMT</pubDate>
    <dc:creator>MikeZdeb</dc:creator>
    <dc:date>2012-01-14T19:27:26Z</dc:date>
    <item>
      <title>how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30000#M5691</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;how can we get the first and last observations in a dataset using Proc SQl?&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 08:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30000#M5691</guid>
      <dc:creator>john83</dc:creator>
      <dc:date>2012-01-14T08:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30001#M5692</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;John,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If your data set has a record number variable that indicates its position in the file, the task is trivial.&amp;nbsp; e.g.,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set sashelp.class;&lt;/P&gt;&lt;P&gt;&amp;nbsp; recnum=_n_;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select *&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from have&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having recnum=min(recnum) or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; recnum=max(recnum)&lt;/P&gt;&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you don't have such a variable, the following works, but uses an undocumented function (i.e., monotonic) and violates the principal that sql isn't supposed to be dependent upon record order:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp; create table want as&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select *&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from sashelp.class&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; having monotonic()=min(monotonic()) or&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; monotonic()=max(monotonic())-1&lt;/P&gt;&lt;P&gt;&amp;nbsp; ;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: Arthur Tabachneck Corrected minor non-code related typing error&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 15:07:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30001#M5692</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2012-01-14T15:07:17Z</dc:date>
    </item>
    <item>
      <title>how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30002#M5693</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks @art297.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 16:58:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30002#M5693</guid>
      <dc:creator>john83</dc:creator>
      <dc:date>2012-01-14T16:58:04Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30003#M5694</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... if you do not have a record number on each observation and you'd rather not use unsupported monotonic(), how about ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;proc sql;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select count(*) into :lastobs from have;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;create table want as&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select * from have (obs=1)&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;union&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select * from have (firstobs=&amp;amp;lastobs);&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 14 Jan 2012 19:27:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30003#M5694</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2012-01-14T19:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30004#M5695</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's a way which uses only documented features.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #000080;"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;&lt;STRONG&gt;sql&lt;/STRONG&gt;&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&lt;SPAN style="color: #0000ff;"&gt;ods&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;output&lt;/SPAN&gt; sql_results=numbered ;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;ods&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;listing&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;close&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;reset&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;number&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&lt;SPAN style="color: #0000ff;"&gt;select&lt;/SPAN&gt; * &lt;SPAN style="color: #0000ff;"&gt;from&lt;/SPAN&gt; sashelp.class &lt;SPAN style="color: #008000;"&gt;/* (obs=1) */&lt;/SPAN&gt; ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;reset&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;nonumber&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;ods&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;listing&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;ods&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;output&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;close&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&lt;SPAN style="color: #0000ff;"&gt;create&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;table&lt;/SPAN&gt; first_last(drop=row) &lt;SPAN style="color: #0000ff;"&gt;as&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;select&lt;SPAN style="color: #000000;"&gt; * &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;from&lt;/SPAN&gt; numbered &lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&amp;nbsp; having row EQ min(row)&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;union&lt;SPAN style="color: #000000;"&gt; all&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #0000ff;"&gt;&lt;SPAN style="color: #000000;"&gt; &lt;/SPAN&gt;select&lt;SPAN style="color: #000000;"&gt; * &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&amp;nbsp; &lt;SPAN style="color: #0000ff;"&gt;from&lt;/SPAN&gt; numbered &lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&amp;nbsp; having row EQ max(row) ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New;"&gt;&lt;SPAN style="color: #0000ff;"&gt;drop&lt;/SPAN&gt; &lt;SPAN style="color: #0000ff;"&gt;table&lt;/SPAN&gt; numbered ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Courier New; color: #000080;"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;SPAN style="color: #000000;"&gt; ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that this will generate two rows if the given data set has one row (test that by un-commenting the OBS= option). If that behavior is not desired, it's simpler; get rid of the UNION and combine, with OR, the MIN and MAX conditions.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Jan 2012 16:13:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30004#M5695</guid>
      <dc:creator>Howles</dc:creator>
      <dc:date>2012-01-15T16:13:09Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30005#M5696</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hpwles.&lt;/P&gt;&lt;P&gt;It is very interesting. I also found the following is working.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;ods listing close ;
ods output sql_results=x ;
proc sql number ;
select * from sashelp.class ; 
quit;
ods listing ;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jan 2012 05:06:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30005#M5696</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2012-01-16T05:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30006#M5697</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi ... neat, so you could use ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;ods listing close ;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;proc sql number ;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select count(*) into :lastobs from sashelp.class;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;ods output sql_results=x (where=(row in (1, &amp;amp;lastobs)));&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;select * from sashelp.class ; &lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;quit;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;ods listing;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and only get one observation in the new data set when the old data set only has one observation&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jan 2012 16:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30006#M5697</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2012-01-17T16:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30007#M5698</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;hi..please find another example...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA cars1;&lt;BR /&gt; INPUT make $ model $ mpg weight price;&lt;BR /&gt;CARDS;&lt;BR /&gt;AMC Concord 22 2930 4099&lt;BR /&gt;AMC Pacer&amp;nbsp;&amp;nbsp; 17 3350 4749&lt;BR /&gt;AMC Spirit&amp;nbsp; 22 2640 3799&lt;BR /&gt;Buick Century 20 3250 4816&lt;BR /&gt;Buick Electra 15 4080 7827&lt;BR /&gt;Cuick Electra 15 4080 7827&lt;BR /&gt;Cuick Century 20 3250 4816&lt;BR /&gt;Cuick Spirit&amp;nbsp; 22 2640 3799&lt;/P&gt;&lt;P&gt;;&lt;BR /&gt;RUN; &lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table test as&lt;BR /&gt;select make,model,mpg,weight,price,monotonic() as rownum,max(calculated rownum) as max,min(calculated rownum) as min,&lt;BR /&gt;case when calculated rownum=calculated max then 1 else 0 end as last_record,&lt;BR /&gt;case when calculated rownum=calculated min then 1 else 0 end as first_record&lt;BR /&gt;from cars1&lt;BR /&gt;group by make&lt;BR /&gt;order by rownum&lt;BR /&gt;;quit;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 12pt; font-family: Courier New;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Mar 2013 20:04:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30007#M5698</guid>
      <dc:creator>sassimple</dc:creator>
      <dc:date>2013-03-28T20:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: how can we get the first and last observations in a dataset using Proc SQl?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30008#M5699</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I must have mis-understood the question&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First row is very easy (obs=1)&lt;/P&gt;&lt;P&gt;Last row not much more fuss (firstobs= &amp;amp;maxobs)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We can collect these data without going through the whole of the (potentially - very big data)&lt;/P&gt;&lt;P&gt;proc SQL ;&lt;/P&gt;&lt;P&gt;select nobs into :maxobs&lt;/P&gt;&lt;P&gt;from dictionary.tables&lt;/P&gt;&lt;P&gt;where libname='YOURLIB' and memname = 'YOUR_TABLE'&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;select * from yourLib.your_table( obs=1 )&lt;/P&gt;&lt;P&gt;union all&lt;/P&gt;&lt;P&gt;select * from&amp;nbsp; yourLib.your_table( firstobs= &amp;amp;maxobs )&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;quit ;&lt;/P&gt;&lt;P&gt;based on sashelp.class, the results viewer shows&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sex&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Age&amp;nbsp;&amp;nbsp;&amp;nbsp; Height&amp;nbsp;&amp;nbsp;&amp;nbsp; Weight&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;___________________________________________&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 8pt; font-family: courier new,courier;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;Alfred&amp;nbsp;&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 69&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 112.5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: courier new,courier;"&gt;William&amp;nbsp;&amp;nbsp; M&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 66.5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 112&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;of course this speedy solution depends on YOURLIB.YOUR_TABLE being a SAS dataset&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ok - did I misunderstand?&lt;/P&gt;&lt;P&gt;peterC&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Mar 2013 02:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-can-we-get-the-first-and-last-observations-in-a-dataset/m-p/30008#M5699</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2013-03-29T02:16:02Z</dc:date>
    </item>
  </channel>
</rss>

