AtuD
AtuD

Reputation: 83

Get total number of observations (rows) in sas dataset (.sas7dat file)

I'm looking for getting the total number of rows (count) from a sas dataset file using SAS code.

I tried this code data _null_; infile "C:\myfiles\sample.sas7bdat" end=eof; input; if eof then put "Lines read=====:" ; run;

This is the results OUTput I get(does not show the number of lines).Obviously, I did not get any actual number of lines in the file

Lines read=====: NOTE: 1 record was read from the infile "C:\myfiles\sample.sas7bdat".

However, I know the number of lines in that sample.sas7dat file is more than 1.

Please help!

Upvotes: 0

Views: 2795

Answers (2)

Stu Sztukowski
Stu Sztukowski

Reputation: 12899

One cool thing about sas7bdat files is the amount of metadata stored with them. The row count of that file is already known by SAS as an attribute. You can use proc contents to read it. Observations is the number of rows in the table.

libname files "C:\myfiles";

proc contents data=files.sample;
run;

A more advanced way is to open the file directly using macro functions.

%let dsid = %sysfunc(open(files.sample) );   /* Open the file */
%let nobs = %sysfunc(attrn(&dsid, nlobs) );  /* Get the number of observations */
%let rc   = %sysfunc(close(&dsid) );         /* Close the file */

%put Total observations: &nobs

Upvotes: 0

Tom
Tom

Reputation: 51566

The INFILE statement is for reading a file as raw TEXT. If you have a SAS dataset then you can just SET the dataset to read it into a data step.

So the equivalent for your attempted method would be something like:

data _null_; 
  set "C:\myfiles\sample.sas7bdat" end=eof; 
  if eof then put "Observations read=====:" _n_ ; 
run;

Upvotes: 2

Related Questions