How to show message if the output is empty after filtering in sas

799 Views Asked by At

I’m trying to show a message or another table with empty record if the result is empty after putting an input.

See below codes:

%if (&number ne) %then %do;

Proc print data=Lib.table;

Var “number”n “name”n “age”n;

Where “number”n=“&number”;

Run;

%end;

The input is number This codes for stored process

Solution This is the solution that worked for me.

%if (&number ne) %then %do; 

    Proc print data=Lib.table; 
        Var “number”n “name”n “age”n; 
        Where “number”n=“&number”; 
    Run; 

    Proc sql; 
        Select case  
                   when count()=0 then “No record found” 
                   Else put (count()),11.) 
               End as Number_of_records 
        From Lib.table 
        where 'number'n="&number"; 
    Quit; 

%end;
1

There are 1 best solutions below

10
On

You could check the table if the value exists, and if it does not, display a proc print saying that it is empty.

%STPBEGIN;

    %if(&number. NE) %then %do;

        proc sql;
            select count(*)
            from table
            where number = &number.
            ;
       quit;

        %if(&sqlobs. = 0) %then %do;
            data message;
                msg = 'No values were returned.';
            run;
    
            proc print data=message;
            run;
        %end;

    %end;

%STPEND;

For this to work, your STP result capabilities need to be set to "Stream."

enter image description here

Note that you can also write HTML to the special fileref _webout to display a message. If you do this, do not enclose the STP with the %STPBEGIN/%STPEND macros or _webout will be write-locked. For example:

data _null_;
    infile datalines;
    file _webout; 

    put _INFILE_;

    datalines4;
<html>
    No data was returned.
</html>
;;;;
run;