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.
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.
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.
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.
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.
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 ....
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.