BookmarkSubscribeRSS Feed
Ujjawal
Quartz | Level 8

I am using %IF %THEN and it works sometimes as expected. Sometimes it does not work as expected.

The following code does not work when i put "mean" in quotes -

options minoperator mlogic;

%macro missing (method=);

%if %lowcase(&method.) = "mean" or %lowcase(&method.) = "median" %then %do;

%put type = "mean or median";

%end;

%else %do;

%put type = "zero";

%end;

%mend;

%missing (method=mean);

When i replace the third line of code to : %if %lowcase(&method.) = mean or %lowcase(&method.) = median %then %do; , the code works.

6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

You likely have no reason to use a quoted-string and it's giving the SAS macro language compilation issues - review the SAS.COM support site DOC / reference material -- search argument:

quoted strings macro language site:sas.com

Scott Barry
SBBWorks, Inc.

Ujjawal
Quartz | Level 8

Thanks for your reply. Sometimes the code works with quotes. I don't understand it. Could you please provide me the link? I searched on sas support site, didn't find the exact link.

Kurt_Bremser
Super User

To help you a little with wrapping your mind about this:

%if %lowcase(&method.) = mean

works


%if "%lowcase(&method.)" = "mean"

will also work


%if %lowcase(&method.) = "mean"

and

%if "%lowcase(&method.)" = mean

won't work.


Since the macro language only knows type char, no quotes are needed for constants.


To further illustrate this, try:

%let macvar="Test";

%put "&macvar";

and look at the log.

Astounding
PROC Star

One principle to keep in mind is that this comparison is false:

%if mean = "mean" %then %do;

Macro language does not require quotes.  Thus this compares four characters (on the left of the equal sign) to six characters (to the right of the equal sign).  In macro language, four characters can never equal six characters.

Tom
Super User Tom
Super User

In regular SAS code you use quotes around literal strings to distinguish them from numeric literals, keywords or variable names. But in macro logic quotes are not treated differently from other characters.  So it is the same as if you tried to test:

%if %lowcase(&method) = XmethodX %then ....

bharathtuppad
Obsidian | Level 7

Macro language only has character datatype. There is no numeric datatype in Macro language. Hence quotations are not needed. If the value has quotes then you have to compare using the quotes else you dont need the quotes.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2087 views
  • 3 likes
  • 6 in conversation