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: 9,681

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,401

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: 9,681

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: 9,681

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;

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 4537 views
  • 0 likes
  • 3 in conversation