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

Why does the first IF Statement below not produce an Error?

 

data test;
x=0; y=1;

if x=0 then z=0
and y=0;

if x=0
and y=0 then w=0;
run;

 

proc print data=test;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
if x=0 then z=0
and y=0;

translates to

if (x = 0) then z = (0 and (y = 0));

(0 and (y = 0)) is a valid numeric expression, and once you look at that, you can see that z will always be set to zero if the condition "x equals zero" is met.

View solution in original post

6 REPLIES 6
PGStats
Opal | Level 21

With SAS the equal sign can be an asignment or an equality operation. z = ... is and assignment because it occurs at the beginning of the statement, but y=0 is an equality operation. SAS represents logical (boolean) values with zeros and ones, 0 = FALSE, 1 = TRUE. The expression

 

z=0 and y=0

 

is computed as

 

z = (0 and (y=0))

z = (0 and (1=0)) /* because y = 1 */

z = (0 and 0) /* because 1=0 is FALSE */

z = 0 /* Because FALSE and FALSE is FALSE */

PG
ballardw
Super User

@stechafle wrote:

Why does the first IF Statement below not produce an Error?

 

data test;
x=0; y=1;

if x=0 then z=0
and y=0;

if x=0
and y=0 then w=0;
run;

 

proc print data=test;
run;


If the intent of the first if is to assign values to both Z and Y the code would be:

if x= 0 then do;

   z=0;

   y=0;

end;

Tom
Super User Tom
Super User

What error should it produce?  

 

Note that the compiler cannot read your mind to know whether it is what you intended or not.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

think about it,

draw pictures with lines results.

then ask yourself do we give order in that manner?

 

no we give clear and understandable orders so a computer understands what the hay you are thinking.

 

Kurt_Bremser
Super User
if x=0 then z=0
and y=0;

translates to

if (x = 0) then z = (0 and (y = 0));

(0 and (y = 0)) is a valid numeric expression, and once you look at that, you can see that z will always be set to zero if the condition "x equals zero" is met.

stechafle
Calcite | Level 5

Thank you Kurt! Your explanation makes perfect sense.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1426 views
  • 2 likes
  • 6 in conversation