Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect how many observations in a dataset (or if it is empty), in SAS?

Tags:

sas

I wonder if there is a way of detecting whether a data set is empty, i.e. it has no observations. Or in another saying, how to get the number of observations in a specific data set.

So that I can write an If statement to set some conditions.

Thanks.

like image 451
mj023119 Avatar asked Apr 14 '11 05:04

mj023119


People also ask

How do you find the number of observations in a data set?

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.

Is there a count function in SAS?

The COUNT function searches string, from left to right, for the number of occurrences of the specified substring, and returns that number of occurrences. If the substring is not found in string, COUNT returns a value of 0.

How do I limit the number of observations in SAS?

You can use the OBS= and FIRSTOBS= data set options to limit the number of observations that SAS processes. The OBS= data set option specifies the number of the last observation to process.

How do you count observations in PROC sql?

In order to find the number of observations or rows in the data file, we will need to use the COUNT function in PROC SQL. COUNT(*) counts all the rows of a table.


2 Answers

It's easy with PROC SQL. Do a count and put the results in a macro variable.

proc sql noprint;
 select count(*) into :observations from library.dataset;
quit;
like image 183
Laurent de Walick Avatar answered Dec 20 '22 05:12

Laurent de Walick


There are lots of different ways, I tend to use a macro function with open() and attrn(). Below is a simple example that works great most of the time. If you are going to be dealing with data views or more complex situations like having a data set with records marked for deletion or active where clauses, then you might need more robust logic.

%macro nobs(ds);
    %let DSID=%sysfunc(OPEN(&ds.,IN));
    %let NOBS=%sysfunc(ATTRN(&DSID,NOBS));
    %let RC=%sysfunc(CLOSE(&DSID));
    &NOBS
%mend;

/* Here is an example */
%put %nobs(sashelp.class);
like image 24
cmjohns Avatar answered Dec 20 '22 03:12

cmjohns