BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi  guys 

 

I have three tables in these three tables suppose i do not know how many observations in each table so

i want to find total observations and extract last five observations from each table at a time how can i write code

8 REPLIES 8
BrahmanandaRao
Lapis Lazuli | Level 10
proc sql ;
select sum(nobs) as total_obs from dictionary.tables 
where libname = 'WORK' and memtype = 'data';
select memname, nobs from dictionary.tables 
where libname = 'WORK';
quit;

In the above code where subtract 4 

please explain

 

Reeza
Super User
That would give you the number of total observations. SAS allows you to access data sets using the FIRSTOBS, so if you take the Sum(nobs), you didn't give it a name, and subtract four that's the first observation you want.

ie

data want;
set sashelp.class (firstobs = 15);
run;

I would recommend Tom's solution since it's the simplest and you can generalize it for your multiple tables as needed. Either way, when processing multiple tables you'll need a macro or loop of some kind to get that working correctly.
jhammouda
Obsidian | Level 7

You could also use proc SQL, substitute the name of your table in myBigtable.  The code should work to get you the last 5 observations of the table.

proc sql;
create table 
Last_Five as
  select *
    from myBigTable (firstobs=%eval(%sysfunc(attrn(%sysfunc(open(MyBigTable)),nlobs))-4))
  ;

quit;
BrahmanandaRao
Lapis Lazuli | Level 10
Thanks for your reply i want last n observations from all datasets in a library how can we do that
Tom
Super User Tom
Super User

@BrahmanandaRao wrote:
Thanks for your reply i want last n observations from all datasets in a library how can we do that

What do you want to DO with the last N observations from these datasets?  Do the datasets all have the exact same structure?  If so you might be able to put the results into a single dataset. Otherwise you will need to either make new datasets with jsut the last N observations or just make some type of report.  So what it is that you want?

japelin
Rhodochrosite | Level 12
%Macro GetLast5(lib,n);
  options dlcreatedir;
  libname out "%sysfunc(pathname(work))\out";
  data _null_;
    set sashelp.vtable(where=(memtype='DATA' and libname=upcase("&lib")));
    call symputx(cats('DS',_n_),memname);
         if nlobs=0   then call symputx(cats('obs',_n_),1);
    else if nlobs=>&n then call symputx(cats('obs',_n_),nlobs-(&n-1));
    else                   call symputx(cats('obs',_n_),nlobs);
    call symputx('nobs',_n_);
  run;

  %do i=1 %to &nobs;
    data out.&&DS&i;
      set &lib..&&DS&i(firstobs=&&obs&i);
    run;
  %end;
%Mend;

/* try extract last 5 obs from sashelp library */ %GetLast5(sashelp,5);
Tom
Super User Tom
Super User

Use the POINT= and NOBS= option.  (Note requires that table does not contain deleted observations since POINT= operates on actual observation number and not logical observation number).

data want;
  do p=max(1,nobs-4) to nobs ;
    set have point=p nobs=nobs;
    output;
  end;
  stop;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 916 views
  • 3 likes
  • 6 in conversation