DATA Step, Macro, Functions and more

%IF Macro condition check with hyphen in argument

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

%IF Macro condition check with hyphen in argument

Dear All,

I have a macro that checks many conditions such as these ones:

%IF %STR(&LEADER) NE %STR() %THEN %DO;

...

%IF %SYSFUNC(FILEEXIST(&PATH.\&SUPERVISOR.\&LEADER.)) %THEN %DO;

In the above, two of the arguments (SUPERVISOR and LEADER) can sometimes have hypens because they are names. When this happens, it yields the following error:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric

       operand is required. The condition was: &LEADER NE

Is there any way around this or should I instruct users to remove hyphens when they call the macro?

Thank you in advance for your help and suggestions,

Daniel


Accepted Solutions
Solution
‎12-11-2013 11:39 AM
Trusted Advisor
Posts: 1,615

Re: %IF Macro condition check with hyphen in argument

Use the %quote function

For example &path\%quote(&supervisor)\%quote(&leader)

View solution in original post


All Replies
Solution
‎12-11-2013 11:39 AM
Trusted Advisor
Posts: 1,615

Re: %IF Macro condition check with hyphen in argument

Use the %quote function

For example &path\%quote(&supervisor)\%quote(&leader)

Super User
Posts: 5,082

Re: %IF Macro condition check with hyphen in argument

An easy way for &LEADER would be:

%if %length(&leader) > 0 %then %do;

This could actually be abbreviated to:

%if %length(&leader) %then %do;

Are you sure that &SUPERVISOR is actually causing a problem?  It looks like it should be OK.

Trusted Advisor
Posts: 1,615

Re: %IF Macro condition check with hyphen in argument

A better way to check to see if a macro variable &leader is blank is:

%sysevalf(%superq(leader)=,boolean)

See http://support.sas.com/resources/papers/proceedings09/022-2009.pdf

This eliminates all possible problems caused by hyphens and other special characters

Occasional Contributor
Posts: 17

Re: %IF Macro condition check with hyphen in argument

Thank you both for your answers, everything works fine with the modifications you have proposed :-)

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 369 views
  • 6 likes
  • 3 in conversation