DATA Step, Macro, Functions and more

%IF %THEN not working

Regular Contributor
Posts: 185

%IF %THEN not working

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


%else %do;

%put type = "zero";



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

Super Contributor
Super Contributor
Posts: 3,176

Re: %IF %THEN not working

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

Scott Barry
SBBWorks, Inc.

Regular Contributor
Posts: 185

Re: %IF %THEN not working

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.

Super User
Posts: 10,278

Re: %IF %THEN not working

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

%if %lowcase(&method.) = mean


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

will also work

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


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

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,785

Re: %IF %THEN not working

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.

Super User
Super User
Posts: 8,120

Re: %IF %THEN not working

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

Posts: 27

Re: %IF %THEN not working

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.

Ask a Question
Discussion stats
  • 6 replies
  • 6 in conversation