DATA Step, Macro, Functions and more

%IF %THEN not working

Reply
Regular Contributor
Posts: 181

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

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

Super Contributor
Super Contributor
Posts: 3,174

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 site:sas.com

Scott Barry
SBBWorks, Inc.

Regular Contributor
Posts: 181

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: 6,939

Re: %IF %THEN not working

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,083

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: 6,500

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

Contributor
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
  • 318 views
  • 3 likes
  • 6 in conversation