BookmarkSubscribeRSS Feed
kmorrowvt
Obsidian | Level 7

I'm trying to figure out why this macro logic is working in this way:

%let bwgt=50;

%macro test;

  %if 0 lt &bwgt and &bwgt lt 50 %then %put test1;

  %if 0 lt &bwgt lt 50 %then %put test2;

%mend test;

%test;

This returns test2.

Why is it returning this when bwgt=50? 

Obviously I can just write my code with an "and" and get around this issue, but I'm curious as to why this is happening

Thanks,

Kate

3 REPLIES 3
Astounding
PROC Star

Unlike a DATA step, macro language does not process this as two separate comparisons:

0 lt &bwgt lt 50

Instead, macro language processes from left to right, as if you had coded:

(0 lt &bwgt) lt 50

Like a DATA step, macro language replaces true comparisons with a 0 and false comparisons with a 1.  Since both 0 and 1 are less than 50, you will always get "test2" for any numeric value of &bwgt even if it's 55.

Good job on prodding, exploring, and asking.

LinusH
Tourmaline | Level 20

Can't fins any references in documentation to that you can use the same between and logic with %if (as you can with if in the data step). But I can't explain why this is happening. You wanted this test to come up as false, right?

Data never sleeps
kmorrowvt
Obsidian | Level 7

Right since bwgt is not gt 0 and lt 50. 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1178 views
  • 4 likes
  • 3 in conversation