Contando registros da tabela

Contando registros na tabela
/* Método trivial - INEFICIENTE */
proc sql;
    select count(*) as N from sashelp.cars;
quit;

/* Utilizando o destritor do dataset - EFICIENTE */
data _NULL_;
    if 0 then set sashelp.cars nobs=n;
    put "no. of observations =" n;

    /* caso queira adicionar numa variável */
    call symputx('totobs',n);

    stop;
run;



/* Utilizando o Proc SQL Dictionary Method - EFICIENTE */
proc sql noprint;
    select nobs into :totobs separated by ' ' from dictionary.tables
    where libname='SASHELP' and memname='CARS';
quit;
%put total records = &totobs.;


/* Verificar se tabela está vazia */
data _NULL_;
    if 0 then set sashelp.cars nobs=n;
    if n = 0 then put 'empty dataset';
    else put 'Not empty. Total records=' n;
    stop;
run;


/* It reads only first two observations from the dataset */
data _NULL_;
    set sashelp.cars nobs=N;
    if _N_ = 2 then stop;
    put N;
run;
  • Caso necessário utilizar essa informação via macro.
Contando registros da tabela
%macro totobs(mydata);
    %let mydataID=%sysfunc(OPEN(&mydata.,IN));
    %let NOBS=%sysfunc(ATTRN(&mydataID,NOBS));
    %let RC=%sysfunc(CLOSE(&mydataID));
    &NOBS
%mend;
%put %totobs(sashelp.cars);




/* Ver se a tabela está vazia */
%macro emptydataset (inputdata=);
    data _NULL_;
        if 0 then set &inputdata. nobs=n;
        call symputx('totobs',n);
        stop;
    run;
    %if &totobs. = 0 %then %put Empty dataset;
    %else %do;
    %put TotalObs=&totobs;
    %end;
%mend;

%emptydataset(inputdata=sashelp.cars);