BookmarkSubscribeRSS Feed
arpitsharma27
Calcite | Level 5

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.*/
7 REPLIES 7
gamotte
Rhodochrosite | Level 12
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
arpitsharma27
Calcite | Level 5

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

gamotte
Rhodochrosite | Level 12
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.
Reeza
Super User

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.

 

SuryaKiran
Meteorite | Level 14

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
Astounding
PROC Star

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.

Tom
Super User Tom
Super User

 

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

 

 

 

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
  • 7 replies
  • 927 views
  • 2 likes
  • 6 in conversation