DATA Step, Macro, Functions and more

How to check if a macro variable is blank

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 128
Accepted Solution

How to check if a macro variable is blank

I tried to do:

data test; test1;

Var1 = Column_&col1.;

run;

But if the "&col" is empty then I would get a error warning in the log file.

Is there a way to check whether my macro variable "&col" is empty?

I tried :

%if %length(&col.) = 0 %then %let col=blank;

but it turned out the %length(&col) = 4;

Any suggestions? Thanks!


Accepted Solutions
Solution
‎03-28-2014 04:10 PM
Super User
Posts: 11,343

Re: How to check if a macro variable is blank

/* macro to test macro parameters             */
/* returns 1 if the tested parameter is blank */
/* 0 otherwise, blank means all charaters are,*/
/* or are macro variables that resolve to a,  */
/* blank                                      */
/* param can be upto 65,531 characters long   */
/* if numeric and several 1000 digits long may*/
/* hang the session. (Windows 32 bit OS)      */
/* NOT a test for a NULL (zero length string) */
/* though may work for some of those as well  */

%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;

Use:

%if %isblank(&var) = 1 %then;

%else ;

or similar

View solution in original post


All Replies
Solution
‎03-28-2014 04:10 PM
Super User
Posts: 11,343

Re: How to check if a macro variable is blank

/* macro to test macro parameters             */
/* returns 1 if the tested parameter is blank */
/* 0 otherwise, blank means all charaters are,*/
/* or are macro variables that resolve to a,  */
/* blank                                      */
/* param can be upto 65,531 characters long   */
/* if numeric and several 1000 digits long may*/
/* hang the session. (Windows 32 bit OS)      */
/* NOT a test for a NULL (zero length string) */
/* though may work for some of those as well  */

%macro isBlank(param);
%sysevalf(%superq(param)=,boolean)
%mend isBlank;

Use:

%if %isblank(&var) = 1 %then;

%else ;

or similar

Frequent Contributor
Posts: 128

Re: How to check if a macro variable is blank

Thanks, BallardW!

But just curious that SAS does not have its own function for this. :smileygrin:

Super User
Super User
Posts: 7,074

Re: How to check if a macro variable is blank

Not sure what you are talking about as if COL1 is totally null (length of zero) or only contains blanks it should not cause any problem with that code.  It might not work as expected since the variable name generated might not match any actual variable, but it will not generate an error.  But then again you would have the same problem with any non empty value.  What could cause an error would be a value that includes a space.  You could test for that using a number of tests.  For example:

%if %sysfunc(countw(&col1,%str( )) > 1 %then %put Value of COL1 contains more than one word ;

Read this paper http://changchung.com/download/022-2009.pdf  for a test that checks if a macro variable is either empty (length of zero) OR contains spaces.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 9328 views
  • 1 like
  • 3 in conversation