Deciding
However, it is not really true that you are not doing any arithmetic on TYPE. The expression in the IF statement is evaluated numerically. SAS looks at the expression: TYPE = '1' and returns a value of either 1 or 0. The value 1 is interpreted as true and 0 as false.
What if TYPE was defined to be a 2-byte character variable?
DATA MIXED; INPUT @20 TYPE $2. @; IF TYPE = '1' THEN ... ... lines ommitted DATALINES; 00134168 1 00245155 1 003 23 220 2 00467180 01 005 35 190 02 ;
Look at the data.
In the first data line, it is apparent that the value of TYPE will be '1 '.
So, will the IF expression return a value of true or false?
In IF TYPE = '1' THEN ...
SAS has to decide: IF '1 ' = '1' THEN ... ?
Rule: When two character values of different length are compared, the shorter is padded with blanks.
So, SAS decides: IF '1 ' = '1 ' THEN ... is true.
So far so good. What about the second data line. What is the value of TYPE?
In the second data line in column 20 there is a space and in column 21 there is a 1.
Rule: When SAS reads a character value, any leading blanks are trimmed off.
So, TYPE will be '1 '.
And the IF expression in the second data line will be true and will run just like the first.
In the third data line TYPE will be '2 '.
And the first IF expression in the third data line will be false. The IF expression after the ELSE will be true and will run just fine.
In the fourth data line TYPE will be '01'.
true or false?
In IF TYPE = '1' THEN ...
SAS has to decide: IF '01' = '1' THEN ... ?
Computers are very literal, you know. The IF expression is false.
Since neither IF expression is true, neither of the two INPUT statements after the first are executed.
In the fifth data line TYPE will be '02'.
Since neither IF expression is true, neither of the two INPUT statements after the first are executed.
The Proc Print output is as follows:
OBS TYPE ID AGE WEIGHT 1 1 1 34 168 2 1 2 45 155 3 2 3 23 220 4 01 . . . 5 02 . . .