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

I have macro x with optional parameters var1, var2.

I call eg. %x(A,B) , %x(,B)

I need to set default value in case eg. var1 is null. To do this I need reliable method to check wether var1 is null or not.

I tried %SYMEXIST(var1) but apparently all macro variables are initialized but with some kind of null value.

I tried %length(var1) =0 but sas says the lenght is >0

Should I use some trim or other command to get rid of some spaces of other "invisible" characters?

Thanks for help

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 characters are,*/
/* or are macro variables that resolve to a  */
/* blank */
/* param can be up to 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;

 

@PaigeMiller also adds:

To see if macro variable is null, Chang CHung and John King examined several different solutions and came to a clear winner

 

http://support.sas.com/resources/papers/proceedings09/022-2009.pdf



 

View solution in original post

14 REPLIES 14
ballardw
Super User

 

/* macro to test macro parameters */
/* returns 1 if the tested parameter is blank */
/* 0 otherwise, blank means all characters are,*/
/* or are macro variables that resolve to a  */
/* blank */
/* param can be up to 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;

 

@PaigeMiller also adds:

To see if macro variable is null, Chang CHung and John King examined several different solutions and came to a clear winner

 

http://support.sas.com/resources/papers/proceedings09/022-2009.pdf



 

Reeza
Super User

You can use the macro logic %if &b= %then to check for a null value.

See the example below.

%let a=test;

%let b=;

%macro test(a, b);

    %if &a = %then %let a=blank;

    %if &b = %then %let b=bblank;

    %put &a. &b.;

%mend;

%test(&a, &b);

Astounding
PROC Star

If you've ever seen a macro that uses a two-letter state code, you know this can be a tricky problem when STATE=OR.

Here's the simple way:

%if %length(&a)=0 %then %do;

Good luck.

Reeza
Super User

I know it can cause issues but odds are OR would be another variable or in quotes so it's usually unlikely.

Jay_TxOAG
Quartz | Level 8

I like to use %if &var1=%str() %then.... just to keep from wondering why there isn't anything after the =

but I have run into problems with quoting with this method...I have started to use %length now...do you find that %length works almost all the time?

Astounding
PROC Star

Jay,

Yes, I find that %LENGTH works almost all of the time.  It can fail if the incoming macro variable contains unbalanced parentheses, although it seems to work if the macro variable contains a semicolon.  I haven't tested it for all other special characters.  I'm also experimenting with another variation:

%length(%superq(var1))

%LENGTH avoids trouble in a few situations.  I already mentioned that the incoming macro variable might take on a keyword value such as OR or NE.  Another situation is where &VAR1 contains a path name that includes an arithmetic trigger.  This generates an error:

%if /path/to/folder = %then %do;

When macro language sees "/" as part of an %IF condition, it assumes it should apply the %EVAL function.  Not a good decision in this case.

The %superq version works in more cases, but generates interesting questions.  What should happen if &VAR1 is created by CALL SYMPUT and contains a few blanks?  Should &VAR1 be considered to be null or not?  Adding %superq changes the results.  And what should happen if CALL SYMPUT assigned &VAR2 as the value of &VAR1.  Should &VAR1 be null when &VAR2 is null?

PaigeMiller
Diamond | Level 26

To see if macro variable is null, Chang CHung and John King examined several different solutions and came to a clear winner

http://support.sas.com/resources/papers/proceedings09/022-2009.pdf

--
Paige Miller
TomKari
Onyx | Level 15

Thanks, Paige

This document was just the thing I needed!

Tom

Jay_TxOAG
Quartz | Level 8

@Astounding - Thanks for the explanation

@PaigeMiller - Thanks for the reference...I couldn't find it at that link, but I found it Here...http://changchung.com/download/022-2009.pdf    

Astounding
PROC Star

Since you said you are already using %LENGTH, check for quoting.  For example, this would get you a length of 3:

%let var = %str(   );

%let len = %length(&var);

%put LEN is &len..;

If that's not it, then examine more closely what is in your macro variable   Try a DATA step approach:

data _null_;

hex_code = put("&var1", $hex12.);

put "&var1  "  hex_code=;

run;

Jay_TxOAG
Quartz | Level 8

did you forget the & before var1?

Tom
Super User Tom
Super User

Macro parameters always exist in the local scope symbol table for the macro.  That is why SYMEXIST will not work for macro parameters.

The answer depends on what you mean by empty.  Personally for macro parameters I usually just test using %LENGTH(&mvar).  This works great for macro parameters as there is no need to worry about macro quoting as any special characters would have to already have been quoted by the user when they called the macro.

If you want to treat a macro parameter that only contains spaces as also being empty then you need a more robust method.  In that case use the method from SUGI the paper that has been sited above in this thread.

PaigeMiller
Diamond | Level 26

And yet in the paper by Chung and King, the test using %length(&mvar) has a drawback and performed worse than the winner found in that paper.

--
Paige Miller
Tom
Super User Tom
Super User

Because that paper has a different definition of NULL and is for testing general macro variables rather than just macro parameters.

Personally to me if someone goes to the effort to use macro quoting in their macro call to pass in a value with non-zero length that is NOT a null parameter value.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 14 replies
  • 97158 views
  • 7 likes
  • 8 in conversation