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!
/* 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
/* 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
Thanks, BallardW!
But just curious that SAS does not have its own function for this. :smileygrin:
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.