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
/* 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/proceeding
s09/022-2009.pdf
/* 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/proceeding
s09/022-2009.pdf
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);
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.
I know it can cause issues but odds are OR would be another variable or in quotes so it's usually unlikely.
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?
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?
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
Thanks, Paige
This document was just the thing I needed!
Tom
@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
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;
did you forget the & before var1?
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.
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.