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 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.
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 */
@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;
What error should it produce?
Note that the compiler cannot read your mind to know whether it is what you intended or not.
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.
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.
Thank you Kurt! Your explanation makes perfect sense.
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!
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.