BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ken_oy
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

/* 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

3 REPLIES 3
ballardw
Super User

/* 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

Ken_oy
Fluorite | Level 6

Thanks, BallardW!

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

Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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