I am not able to resolve the cause of the error. Can someone please help me.
Here is my code:
%macro chk(where=);
%if %isblank(%nrstr(&where.))=0 %then
%do;
%if %upcase(%substr(%str(&where.),1,5)) ne WHERE %then
%do;
%put %str(E)RROR: "Please put WHERE at the starting of the where macro parameter.";
%return;
%end;
%end;
%mend;
options symbolgen mprint mlogic;
%chk(where=where);/*It works as Expected*/
%chk();/*It does NOT work as Expected. I want the first condition to turn to false for this case.*/
/*Also tried this*/
%macro chk(where=);
%if not %isblank(%nrstr(&where.)) %then
%do;
%if %upcase(%substr(%str(&where.),1,5)) ne WHERE %then
%do;
%put %str(E)RROR: "Please put WHERE at the starting of the where macro parameter.";
%return;
%end;
%end;
%mend;
%chk(where=where);/*It works as Expected*/
%chk();/*It does NOT work as Expected. I want the first condition to turn to false for this case.*/
So I see your point in helping me resolve the issue here.-- By using %length and determining the macro variable. And it does solve my problem.
But why is the first condition not resolving to FALSE.?
Using = or just listing parameter is two different ways of defining a macro ie
%macro myMacro(var1= , var2=);
Is different than:
%macro myMacro(var1, var2);
They're both valid, but different definitions. For example, in the first one the order of the parameters doesn't matter when you call it.
%macro myMacro(var1=, var2=);
proc print data=sashelp.class (obs=3);
var &var1 &var2;
run;
%mend;
%mymacro(var1=age, var2=name);
%mymacro(var2=name, var1=age);
%mymacro(var1=name, var2=age);
But whatever option you pick, you must stick with it. In your question above, you're mixing styles.
Your macro statement is defined by keyword parameter and your trying to invoke it with not parameters, then default values will be applied (%chk(); or %chk; then will be resolved as %chk(where=); )
I'd suggest getting rid of %ISBLANK and just checking for a null value in a simple way:
%if %length(&where.) > 0 %then %do;
At least that's what I assume %ISBLANK is supposed to be doing.
I suspect the problem is from the use of %NRSTR that could prevent resolution of &WHERE. (but I can't check that right now).
Similarly, it looks like %STR does nothing when encased within %UPCASE and %SUBSTR. I would remove %STR from that line of code.
Here is a macro that will add the keyword WHERE to the beginning if the value does not already start with the word WHERE.
%macro chk(where);
%if %length(&where) %then %do;
%if "WHERE " ne "%qsubstr(%qupcase(&where) 12345,1,6)" %then
%let where=where &where
;
%end;
%put &=where ;
%mend;
%chk();
%chk(fred);
%chk(wherefred);
%chk(where age>10);
The normal ISBLANK macro I have seen tests if the value is empty or only contains spaces. I normally just use %LENGTH() to test if the string is empty. To see the difference try calling the test macro with a macro quoted blanks.
%chk(%str( ));
Note that you can defined the parameter as positional so that you can call the macro without listing the parameter name. But you can still call it using the keyword if you want. You just can't call by position when the parameter is defined as keyword.
%chk(where=where age>10);
%chk(where=age>10);
Example results:
88 %chk(); WHERE= 89 %chk(fred); WHERE=where fred 90 %chk(wherefred); WHERE=where wherefred 91 %chk(where age>10); WHERE=where age>10 92 %chk(%str( )); WHERE=where 93 %chk(where=where age>10); WHERE=where age>10 94 %chk(where=age>10); WHERE=where age>10
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.
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.