Number of observations
The number of observations collected in a SAS library can be a helpful when it is available as a macro variable. When the number of observations is different than expected, the programmer can dynamically include additional code or stop the SAS step with diagnostic messages.
The NOBS parameter of the SAS SET statement gives the number of observations in a SAS library. One helpful hint is that the file needs to be opened but not totally read. So to get the number of observations can be accomplished by reading only the first observation of the file.
Proc sql can also get the number of observations from SQL files and SAS files. The SAS Fifth Dimension Macro uses the proc sql technique.
Data Step - Number of observations of last updated SAS file.
%let _NUMOBS_=0 ; DATA _NULL_ ; SET &SYSLAST NOBS = NUMOBS ; IF _N_ > 1 THEN STOP ; CALL SYMPUT('_NUMOBS_',NUMOBS) ; RUN ;
SAS Fifth Dimension Macro-NOBS
|DSN||The SAS file name.|
%MACRO NOBS(DSN) ; %GLOBAL _NUMOBS_ ; %LET _NUMOBS_=0 ; proc sql noprint ; select count(*) into :_NUMOBS_ from &dsn ; quit ; %PUT %STR(----------->NUMOBS OF &DSN=%TRIM(&_NUMOBS_)) ; RUN ; %MEND ;
Note: The NOBS macro will list in the SAS log the number of records in the file
In this example, when the Input file does not contains any records the macro variable _NUMOBS_ = 0 and a message is sent to the sas log and SAS is Stopped.
DATA TEST ; INFILE TEST ; INPUT @1 VAR1 $10. ; %NOBS ; IF &_NUMOBS_<1 THEN DO ; %PUT "------->SAS is stopping since file contains 0 records" ; ENDSAS ; END ;