Check whether a macro variable is empty or not.
%let res=%macro_isblank(var, verb=no);
Arguments
var
: name of a variable to evaluate the content of; verb
: (option) see %error_handle arguments; default: no
.
Returns
res
: evaluated boolean condition, either true (1) when the variable is blank/not set (i.e. when var
is for instance '', or "", or (), but also " ", or ' ', ...), or false (0) otherwise.
Examples
%let var = 0;
%let res=%macro_isblank(var);
returns res=0
, while running:
%let var = ;
%let res=%macro_isblank(var);
returns res=1
. Ibid for the following example:
%let parameter = ' ';
%let res=%macro_isblank(parameter);
which returns res=1
.
Run macro %_example_macro_isblank
for more examples.
Note
- When using the macro
macro_isblank
, keep in mind the following (arbitrarily chosen) outputs:
- variables " ", ' ' and ( ) are considered as empty/blank variables, whatever the number of 'blanks' inside the string, e.g. " " is also considered as empty/blank;
- on the contrary, single quotes (i.e., set to %str(%") and %str(') respectively) are considered as NON empty/blank variables.
- Let us further note that:
%let ans=%macro_isblank();
will return ans=1
.
Note
In the reference below, the authors recommend the use of:
%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;
while they also evaluate:
%if ¶m eq %then ...
%if %bquote(¶m)= %then ...
%if %nrbquote(¶m)= %then ...
%if %superq(param)= %then ...
%if "¶m" = "" %then ...
%if %length(¶m) = 0 %then ...
%if %length(%qleft(%qtrim(¶m))) = 0 %then ...
References
- Carpenter, A.L. (1997): "Resolving and using &&var&i macro variables".
- Carpenter, A.L. (2005): "Five ways to create macro variables: A short introduction to the macro language".
- Philp, P. (2008): "SAS MACRO: Beyond the basics".
- Chang et al.(2009): "Is this macro parameter blank?".
- Wilson, S.A. (2011): "The validator: A macro to validate parameters".
- Weston, K. (2011): "Ways of creating macro variables".
See also
%error_handle, SUPERQ.