How to count the number of observations in the SAS table?

I am very new to SAS. Now I have a SAS data table:

ID score ------------------- 01 1 02 3 03 4 04 2 

Is it possible to save the number of observations in this table using only the PROC SORT and DATA steps ? I want to save a value in a log window, which is similar to "hold N = 4" in a SAS log script.

Sorry for the unprofessional description. Thanks in advance.

+7
statistics sas
source share
3 answers

Use nobs = in the set statement.

 data _null_; set xyz nobs=nobs; put "HOLD N=" nobs ; stop; run; 
+2
source share

As a new SAS user, the NOBS parameter may be all you need. However, as your coding skills increase, you may find yourself in situations where this is not appropriate. The NOBS parameter in the SET statement may not work in all cases. The return value will be the number of physical observations in the dataset, including any observations that might have been removed in place. It may also not work with certain views (especially views related to external databases).

The β€œsafest” way to find the number of recovered cases in a dataset or view is to use PROC SQL and actually count them by placing the result in a macro variable. For example, suppose you have a data object named HAVE:

 proc sql noprint; select count(*) into : nobs from WORK.HAVE; quit; %put 'Obs in data set:' &nobs; 

Note that this works if HAVE is a dataset or view.

Alternatively, if you are an object of only a dataset, you can use the SAS TABLES Dictionary view to return the NLOBS attribute, which has a number of β€œlogical” observations (that is, accounting for all deleted rows):

 proc sql noprint; select nlobs into : nobs from dictionary.tables where libname='WORK' and memname='HAVE'; quit; %put 'Obs in data set:' &nobs; 

This will certainly be more effective if your SAS dataset is very large. I often wondered why SAS does not make this NLOBS value available as an option in the SET statement, but I'm sure there are reasons.

PROC SQL, views, macro variables and in-place remote observations may be completely new to you right now, but as you progress with SAS training, you will definitely start using them.

+8
source share

data null;

install sashelp.vtable;

where libname = "WORK" and memname = "DS1";

call symput ("count_obs", nlobs);

to run;

% put obs in ds1: & count_obs;

-2
source share

All Articles