BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KCKC
Calcite | Level 5

%macro v3(datain,flaga,flagb);
%if &flaga = LT %then
%do;
%put &flaga;
%end;
%else
%do;
%put &flagb;
%end;
%mend v3;

%v3(aa,LT,error);

I always get error instead of LT in this case. Why?

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Try it with quotes.  i.e.,

%macro v3(datain,flaga,flagb);

%if "&flaga" = "LT" %then

%do;

%put &flaga;

%end;

%else

%do;

%put &flagb;

%end;

%mend v3;

%v3(aa,LT,error)

and, since you're probably going to ask "why?", I think it is because LT is being considered an operator.  I'd call this a bug, but I'm sure that someone else will say it is a feature.

You will get the desired results by not using an operator as the value.  e.g.:

%macro v3(datain,flaga,flagb);

%if &flaga. = TT %then

%do;

%put &flaga;

%end;

%else

%do;

%put &flagb;

%end;

%mend v3;

%v3(aa,TT,error)

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

Try it with quotes.  i.e.,

%macro v3(datain,flaga,flagb);

%if "&flaga" = "LT" %then

%do;

%put &flaga;

%end;

%else

%do;

%put &flagb;

%end;

%mend v3;

%v3(aa,LT,error)

and, since you're probably going to ask "why?", I think it is because LT is being considered an operator.  I'd call this a bug, but I'm sure that someone else will say it is a feature.

You will get the desired results by not using an operator as the value.  e.g.:

%macro v3(datain,flaga,flagb);

%if &flaga. = TT %then

%do;

%put &flaga;

%end;

%else

%do;

%put &flagb;

%end;

%mend v3;

%v3(aa,TT,error)

Astounding
PROC Star

Yes, that's right.  Within a %IF condition, LT = "less than".

Macro language makes comparisons from left to right.  The first comparison within:

%if &flaga = LT

compares &flaga to the null value before "LT" to see if they are equal.  Macro language finds that they are not equal, so the comparison is false.  False comparisons generate a 0, while true comaprisons generate a 1.  So the second comparison compares 0 to see if it is less than the null value following "LT".  That comparison is also false, so the %ELSE statement kicks in.  To verify that this is happening, try using the same parameters, but modifying the %IF statement:

%if &flaga = LT 2 %then %do;

Regardless of the value of &flaga, this comparison should always be true.  Macro language will always be comparing either (0 LT 2) or (1 LT 2).

Hope this helps rather than confuses!

KCKC
Calcite | Level 5

Folks, thanks a lot!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 541 views
  • 3 likes
  • 3 in conversation