SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Guys  

Good Morning  

How to find last record in a dataset using proc sql 

 

data last ;
set sashelp.class end=last;
if last then output;
proc print;run;
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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.

 

Instead, you can do your actual example like this:

proc sql;
  create table last as 
  select * from sashelp.class
  having name=max(name)
  ;
quit;

As the data in SASHELP class are sorted by NAME.

 

Another way to "cheat" is to use the FIRSTOBS option:

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=&nobs);
quit;

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.

 

The same thing can be done in a data step like this:

Data last;
  set sashelp.class nobs=nobs point=nobs;
  output;
  stop;
run;

and this is also a lot faster than reading the whole table like in your example.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

@BrahmanandaRao wrote:

Hi Guys  

Good Morning  

How to find last record in a dataset using proc sql 

 

data last ;
set sashelp.class end=last;
if last then output;
proc print;run;

The concept of order of observations is foreign to SQL.  It is a set operation language.

Kurt_Bremser
Super User

The correct answer is, as usual, Maxim 14.

SQL has no out-of-the-box tools for sequences, so you use the data step for such operations.

You can try a trick using the undocumented MONOTONIC function:

proc sql;
create table want (drop=m) as
  select
    *,
    monotonic() as m
  from sashelp.class
  having m = max(m)
;
quit;

(untested, posted from my tablet)

But I would NEVER use this in real life code.

BrahmanandaRao
Lapis Lazuli | Level 10
Thanks for your solution
s_lassen
Meteorite | Level 14

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.

 

Instead, you can do your actual example like this:

proc sql;
  create table last as 
  select * from sashelp.class
  having name=max(name)
  ;
quit;

As the data in SASHELP class are sorted by NAME.

 

Another way to "cheat" is to use the FIRSTOBS option:

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=&nobs);
quit;

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.

 

The same thing can be done in a data step like this:

Data last;
  set sashelp.class nobs=nobs point=nobs;
  output;
  stop;
run;

and this is also a lot faster than reading the whole table like in your example.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 14010 views
  • 1 like
  • 4 in conversation