DATA Step, Macro, Functions and more

How to count the number of observations in a data frame

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to count the number of observations in a data frame

Hello, I am using SAS Enterprise Guide. I'd like to do some quick exploratory analysis of a data set that's already loaded in my work library, however when I run the code below I get the error message "Statement is not valid or is used out of proper order".

 

NROW(dataset);

I would also be very interested in learning what people's favorite EDA functions are, wish there was a SAS equivalent of the str() function in R.


Accepted Solutions
Solution
‎07-05-2017 08:40 AM
Super User
Posts: 10,023

Re: How to count the number of observations in a data frame

[ Edited ]

Yeah. NROW(x) is IML code. if you are familiar with R ,then IML is every familiar with you . Here are two ways to know how much number of obs there are in a data frame/dataset/table :

 

/* 1) IML code: */
proc iml;
 use sashelp.class nobs nobs;
 read all var{age} into x;
 close;

 print nobs;
 print (nrow(x));
quit;

/* 2) Data Step: */

data _null_;
 set sashelp.class nobs=nobs;
 put nobs=;
 stop;
run;

/* 
  proc contents can also give you 
  all the information about that data frame / table . 
*/

proc contents data=sashelp.class varnum;
run;

 

 

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: How to count the number of observations in a data frame

[ Edited ]

Hi,

 

Errm, NROW() is a SAS IML function, needs to be in an IML statement.  There are many ways to find out number of observations, you could open the dataset and look, inspect its properties, do a data set statement and look at the log, proc sql count(*), proc means, proc freq, proc tabulate etc.

For EDA - see above procedures.  The R str() function returns attributes on the data yes?  If so then proc contents does the same, or look at the vcolumn/table in sashelp.library.

 

To add, do you know SAS at all?  You will need to learn the language and way of working to be able to pretty much anything, much like any programming language.

Occasional Contributor
Posts: 15

Re: How to count the number of observations in a data frame

My IT dept just confirmed we don't have a license for IML, which is why it wasn't working. Didn't realize there was different SAS functions for their different products.

Super User
Posts: 10,023

Re: How to count the number of observations in a data frame

proc contents can give you all the information about that data frame / table .


proc contents data=sashelp.class varnum;
run;
Solution
‎07-05-2017 08:40 AM
Super User
Posts: 10,023

Re: How to count the number of observations in a data frame

[ Edited ]

Yeah. NROW(x) is IML code. if you are familiar with R ,then IML is every familiar with you . Here are two ways to know how much number of obs there are in a data frame/dataset/table :

 

/* 1) IML code: */
proc iml;
 use sashelp.class nobs nobs;
 read all var{age} into x;
 close;

 print nobs;
 print (nrow(x));
quit;

/* 2) Data Step: */

data _null_;
 set sashelp.class nobs=nobs;
 put nobs=;
 stop;
run;

/* 
  proc contents can also give you 
  all the information about that data frame / table . 
*/

proc contents data=sashelp.class varnum;
run;

 

 

New Contributor
Posts: 2

Re: How to count the number of observations in a data frame

/* Here's a trick I occasionally use. Note only one record is read form each dataset but SAS magic knows all… */


Data my_dataset ;
   do I=1 to 27 ;
       output ;
   end ;
run ;

 

Data my_dataset2 ;
   do I=1 to 73 ;
      output ;
   end ;
run ;

 

data _null_ ;
    set my_dataset(obs=1) nobs=nobs ;
    call symputx('records',nobs) ;
run ;
%put records: &records ;

 

/* Here SAS knows how many obs are in the concatenated datasets too… */
data _null_ ;
    set my_dataset(obs=1) my_dataset2(obs=1) nobs=nobs ;
    call symputx('records',nobs) ;
run ;
%put Concatenated records: &records ;

 

The Log file:
NOTE: The data set WORK.MY_DATASET has 27 observations and 1 variables.


NOTE: The data set WORK.MY_DATASET2 has 73 observations and 1 variables.

NOTE: There were 1 observations read from the data set WORK.MY_DATASET.

 

records: 27

 

NOTE: There were 1 observations read from the data set WORK.MY_DATASET.
NOTE: There were 1 observations read from the data set WORK.MY_DATASET2.

Concatenated records: 100

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 9703 views
  • 1 like
  • 4 in conversation