BookmarkSubscribeRSS Feed
Jakkas
Calcite | Level 5

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.

6 REPLIES 6
Linlin
Lapis Lazuli | Level 10

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

Astounding
PROC Star

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.

Peter_C
Rhodochrosite | Level 12

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;

data_null__
Jade | Level 19

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.
Peter_C
Rhodochrosite | Level 12

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.

Florent
Quartz | Level 8

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 1453 views
  • 1 like
  • 6 in conversation