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

后端 未结 7 1258
慢半拍i
慢半拍i 2020-11-29 04:47

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 d

7条回答
  •  误落风尘
    2020-11-29 05:14

    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);
    

提交回复
热议问题