SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Is there a simple way to get a count of the total number of records in a table, perhaps when using Proc SQL?

Thanks in advance....looking forward to your replies!
1 ACCEPTED SOLUTION

Accepted Solutions
MohamedS
Obsidian | Level 7

Hi,

You can use the function:

select count(*) as countrec from tablename;

within your proc sql.

 

If the table is a SAS dataset, it has its size in the header and is accessible with the NOBS= option:

%macro get_table_size(inset,macvar);
 data _null_;
  set &inset NOBS=size;
  call symput("&macvar",size);
 stop;
 run;
%mend;


This method takes O(k) time (that is constant time) and doesn't require scanning through the table, O(n), to count the records.  Use it like this:

 

%let reccount=;
%get_table_size(sashelp.cars,reccount);
%put &=reccount;

View solution in original post

10 REPLIES 10
MohamedS
Obsidian | Level 7

Hi,

You can use the function:

select count(*) as countrec from tablename;

within your proc sql.

 

If the table is a SAS dataset, it has its size in the header and is accessible with the NOBS= option:

%macro get_table_size(inset,macvar);
 data _null_;
  set &inset NOBS=size;
  call symput("&macvar",size);
 stop;
 run;
%mend;


This method takes O(k) time (that is constant time) and doesn't require scanning through the table, O(n), to count the records.  Use it like this:

 

%let reccount=;
%get_table_size(sashelp.cars,reccount);
%put &=reccount;
deleted_user
Not applicable
Mohamed,

Thanks for your reply! That worked for what I wanted to know about my dataset.
deleted_user
Not applicable
If the table is a SAS dataset, it has its size in the header and is accessible with the NOBS= option:

%macro get_table_size(inset,macvar);

data _null_;
set &inset NOBS=size;
call symput("&macvar",size);
stop;
run;
quit;

%mend;

This method takes O(k) time (that is constant time) and doesn't require scanning through the table, O(n), to count the records.

To make the macro more robust, you would want to add a conditional wrapper:

%if %sysfunc(exist(&inset)) %then %do;


and then have some appropriate %else %do; for when the &inset doesn't "exist".

I'm not sure if there is a generic way to distinguish a permanent SAS dataset from a database table. I don't know if NOBS= will work for a database (Oracle, DB2, Sybase, Access, SQL Server, MySQL, etc. ). Message was edited by: Chuck
deleted_user
Not applicable
Chuck,

Interesting.... and thank you for the information. I'll put that to good use as time progresses.
deleted_user
Not applicable
in proc sql, you could also go against a table's metadata in dictionary.tables to get the nobs in a data set:
proc sql;
select nobs
from dictionary.tables
where libname eq 'SASHELP'
and memname eq 'ZIPCODE';
quit;

this approach avoids actually reading the data set. if you're looking at a very large table, this can save lots of time; if you're accessing a remote library, it should also save time. this only appears to work for sas data sets and not for databases (sql, mysql, oracle, etc).

i've also been unsuccessful with my attempts to access the view sashelp.vtable:
data _null_;
set sashelp.vtable;
where libname eq 'SASHELP'
and memname eq 'ZIPCODE';
put nobs;
run;
deleted_user
Not applicable
And thank you also, Richard. So many ways to do the same thing!
deleted_user
Not applicable
I have no knowledge of macros, so I would like to know how to use the get_table_size macro.

If I have a data step:

data new_table;
set old_table;



run;

is it possible to call the macro in the same data step and assign the new_table number of rows to a variable nRows.

I would like to see the macro call:

nRows=%get_table_size( ?, ? ); /* Something like that? */
deleted_user
Not applicable
Chuck,
on a more basic level, what does the "&macvar" stand for? is this the name of the new variable you are creating which contains the total # of records?

thanks,
abc
Peter_C
Rhodochrosite | Level 12
for sas information about a table in a dbms, see corresponding rows in sashelp.vmember and sashelp.vviews.
However, each dbms has its database information files equivalent to dictionary.tables. these probably could advise number of rows and columns (last time stats were collected). these resources are database dependant.

PeterC
RichardH_sas
SAS Employee
Since this is an EG forum, I'll mention that in EG you can right-click on a table and see the number of rows under Properties in the General pane. If it's a DMBS table and not a SAS data set, by default this will say unknown. You can change that under Tools \ Options \ Data General then check "Always obtain the total record count for DBMS tables". There could be performance issues with this, FYI, which is why it's not checked by default.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 10 replies
  • 110947 views
  • 5 likes
  • 4 in conversation