<?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 to  find last record using  proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744726#M233345</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Guys&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Morning&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to find last record in a dataset using proc sql&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last ;
set sashelp.class end=last;
if last then output;
proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The concept of order of observations is foreign to SQL.&amp;nbsp; It is a set operation language.&lt;/P&gt;</description>
    <pubDate>Mon, 31 May 2021 04:47:21 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-31T04:47:21Z</dc:date>
    <item>
      <title>How to  find last record using  proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744723#M233343</link>
      <description>&lt;P&gt;Hi Guys&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Morning&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to find last record in a dataset using proc sql&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last ;
set sashelp.class end=last;
if last then output;
proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 31 May 2021 04:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744723#M233343</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-05-31T04:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to  find last record using  proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744726#M233345</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Guys&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good Morning&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to find last record in a dataset using proc sql&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data last ;
set sashelp.class end=last;
if last then output;
proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The concept of order of observations is foreign to SQL.&amp;nbsp; It is a set operation language.&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 04:47:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744726#M233345</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-31T04:47:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to  find last record using  proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744727#M233346</link>
      <description>&lt;P&gt;The correct answer is, as usual, Maxim 14.&lt;/P&gt;
&lt;P&gt;SQL has no out-of-the-box tools for sequences, so you use the data step for such operations.&lt;/P&gt;
&lt;P&gt;You can try a trick using the undocumented MONOTONIC function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want (drop=m) as
  select
    *,
    monotonic() as m
  from sashelp.class
  having m = max(m)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested, posted from my tablet)&lt;/P&gt;
&lt;P&gt;But I would NEVER use this in real life code.&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 04:52:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744727#M233346</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-31T04:52:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to  find last record using  proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744732#M233350</link>
      <description>&lt;P&gt;SQL by design does not care about the record order. So there is no inherent way to get the "last" record. You may "cheat" by using the MONOTONIC function, but there is no guarantee the records will be retrieved in the order they have on the table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, you can do your actual example like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table last as 
  select * from sashelp.class
  having name=max(name)
  ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As the data in SASHELP class are sorted by NAME.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another way to "cheat" is to use the FIRSTOBS option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select nobs into :nobs from dictionary.tables
  where libname='SASHELP' and memname='CLASS';
  create table last as 
  select * from sashelp.class(firstobs=&amp;amp;nobs);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which is a lot more efficient than using MONOTONIC, if you only want to get the last observation, and also faster than my first example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The same thing can be done in a data step like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data last;
  set sashelp.class nobs=nobs point=nobs;
  output;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and this is also a lot faster than reading the whole table like in your example.&lt;/P&gt;</description>
      <pubDate>Mon, 31 May 2021 06:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744732#M233350</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-05-31T06:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to  find last record using  proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744735#M233351</link>
      <description>Thanks for your solution</description>
      <pubDate>Mon, 31 May 2021 06:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-last-record-using-proc-sql/m-p/744735#M233351</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2021-05-31T06:49:16Z</dc:date>
    </item>
  </channel>
</rss>

