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

Hi there, 

I am learning a macro program. I am confused about multiple operators. I don't know how SAS evaluates %if comparisons with two operators, one is "(&brand. = Flase)", the other is ">=.". the result of this program returned  "Brand is General Electric". may I know why? how to decompose multiple operators?

.

%macro corpare_brand(brand);
%if ((&brand. = Flase) >=. ) %then %put Brand is General Electric;
%else %put Brand is &brand;
%mend;
%corpare_brand(Coke)

 

thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS uses %EVAL() to evaluate the condition in a %IF statement.

So just ask %EVAL() what it thinks of those tests.

1009  %put %eval( Coke = Flase );
0
1010  %put %eval(0 >= .);
1

 

the test:

%if ((&brand. = Flase) >=. ) %then 

Is always TRUE.

Do you see why?

Spoiler
The result of a boolean expression is either TRUE (1) or FALSE (0). 

If %EVAL() decided to compare them as numbers then both 0 and 1 are larger than a missing value .

And if %EVAL() decided to do a character comparison both '30'x and '31'x are greater than '2E'x.

 

View solution in original post

7 REPLIES 7
SASKiwi
PROC Star

If you are just testing equality this works:

%macro corpare_brand(brand);
%if &brand. = Flase %then %put Brand is General Electric;
%else %put Brand is &brand;
%mend;
%corpare_brand(Coke);
Alexx_
Calcite | Level 5
yes, I know that works. but "%if &brand. = Flase" returned "Brand is Coke". But I want to know why "%if ((&brand. = Flase) >=. )" returned "Brand is General Electric"
Alexx_
Calcite | Level 5

the original macro program showed below:

%macro compare_brand(brand);
    %if &brand = GE %then %put Brand is General Electric;
   %else %put Brand is &brand;
   %mend;
%compare_brand(Coke)

Astounding
PROC Star
This statement doesn't compare anything to the string GE. In macro language, GE means "greater than or equal to".

The easiest workaround would be to add double quotes to the comparison:

%if "&brand" = "GE" %then %put Brand is General Electric;
SASKiwi
PROC Star

What are you trying to do with this -"%if ((&brand. = Flase) >=. )" ? Some sort of Boolean comparison?

Tom
Super User Tom
Super User

SAS uses %EVAL() to evaluate the condition in a %IF statement.

So just ask %EVAL() what it thinks of those tests.

1009  %put %eval( Coke = Flase );
0
1010  %put %eval(0 >= .);
1

 

the test:

%if ((&brand. = Flase) >=. ) %then 

Is always TRUE.

Do you see why?

Spoiler
The result of a boolean expression is either TRUE (1) or FALSE (0). 

If %EVAL() decided to compare them as numbers then both 0 and 1 are larger than a missing value .

And if %EVAL() decided to do a character comparison both '30'x and '31'x are greater than '2E'x.

 

Alexx_
Calcite | Level 5
thanks @Tom

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 684 views
  • 1 like
  • 4 in conversation