DATA Step, Macro, Functions and more

Macro Parameter

Reply
Occasional Contributor
Posts: 6

Macro Parameter

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.*/
Super Contributor
Posts: 329

Re: Macro Parameter

Posted in reply to arpitsharma27
Hello,

In your second call, &where. is an empty string so you can't extract the first five characters (the log displays an error). You can check length prior to the substring extraction or add characters to ensure that your string is at least 5 characters long :
%if %upcase(%substr(%str(&where.12345),1,5)) ne WHERE
Occasional Contributor
Posts: 6

Re: Macro Parameter

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.?

Super Contributor
Posts: 329

Re: Macro Parameter

Posted in reply to arpitsharma27
The isblank macro is not part of the base sas language so it must probably be user defined. Without the code, it is not possible to answer your question.
Super User
Posts: 23,224

Re: Macro Parameter

Posted in reply to arpitsharma27

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.

 

Valued Guide
Posts: 556

Re: Macro Parameter

[ Edited ]
Posted in reply to arpitsharma27

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=);  )

Thanks,
Suryakiran
Super User
Posts: 6,622

Re: Macro Parameter

Posted in reply to arpitsharma27

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.

Super User
Super User
Posts: 7,929

Re: Macro Parameter

Posted in reply to arpitsharma27

 

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

 

 

 

Ask a Question
Discussion stats
  • 7 replies
  • 110 views
  • 2 likes
  • 6 in conversation