DATA Step, Macro, Functions and more

Macro variable with value OR in %IF Condition

Reply
Occasional Contributor
Posts: 16

Macro variable with value OR in %IF Condition

Hello,

     I have a small macro which conditionally executes code bases on state abbreviation. I want to execute some code for Oregon (State abbreviation OR). The code I am having trouble with is the following:

%MACRO Test(ST);
     %IF &ST. = OR %THEN %DO;
           %PUT State is Oregon.;
     %END;
     %ELSE %DO;
           %PUT State is not Oregon.;
     %END;
%MEND;

%Test(OR);

I think the issue is because OR is a keyword. The macro works fine for PA, or IN and so on but not for OR. Is there a quoting function or something I can use to overcome this issue? Thank you.

Super Contributor
Posts: 1,636

Re: Macro variable with value OR in %IF Condition

try:

%MACRO Test(ST);

     %IF &ST. = "OR" %THEN %DO;

           %PUT State is Oregon.;

     %END;

     %ELSE %DO;

           %PUT State is not Oregon.;

     %END;

%MEND;

%Test("PA");

Super User
Posts: 5,069

Re: Macro variable with value OR in %IF Condition

A small tweak to the suggested solution:  double quotes on both sides.

%if "&st." = "OR" %then %do;

Macro quoting functions could be used, but at least for this simple test the double quotes will work fine.

Valued Guide
Posts: 2,174

Re: Macro variable with value OR in %IF Condition

macro quoting to protect against finding a variable resolves to what looks like logic is straightforward in this case.

rather that quotes use the function %str()

%if %str(&st) EQ %str(OR) %then %do;

  /* whatever you need to do for OR */

%end;

Respected Advisor
Posts: 3,777

Re: Macro variable with value OR in %IF Condition

I was thinking that %STR(&ST) would not be the right function but it does work when the value of ST=OR.  Guess I need to RTFM.

I think using and execution time function is safer.

19            %if %superq(st) eq %str(OR) %then %do;
20               %PUT ST=&st;
21               %end;
22            %else %put not or;
23            %if %str(&st) eq %str(OR) %then %do;
24               %PUT ST=&st;
25               %end;
26            %else %put not or;
27            %mend;
28        
29         %ck(st=|)
not or
ERROR: A character operand was found in the
%EVAL function or %IF condition where a numeric operand is required. The condition was:
       &st eq OR
ERROR: The macro CK will stop
executing.
Valued Guide
Posts: 2,174

Re: Macro variable with value OR in %IF Condition

I can't be giving data_null_; explanations I'm used to those flowing in the other direction!

here %str() is suitable because of the limited range of values that &ST might take - all state abbreviations. the time when extra protection is necessary occurs when the macro variable might contain special symbols not "protected" by %str(). These special symbols % and & would be well protected by%superq() but might need no more than %nrstr() - depending on the timing required for resolving values.

IN general defensive programming recommends %superq() as data_null; suggested here.

%Str() is adequate on this occasion because we know what range of values &ST will take.

Frequent Contributor
Posts: 127

Re: Macro variable with value OR in %IF Condition

The following code works:

%MACRO Test(ST);

     %IF "&ST" = "OR" %THEN %DO;

           %PUT State is Oregon.;

     %END;

     %ELSE %DO;

           %PUT State is not Oregon.;

     %END;

%MEND;

%Test(OR);

Ask a Question
Discussion stats
  • 6 replies
  • 300 views
  • 1 like
  • 6 in conversation