Recently when working on a previous SAS-L question I stumbled across the
following surprise in the way SAS determines whether a variable is character
If I code the below simple DOW-loop everything is fine.
input Var1 $1;
do until (var1='A');
But if I change the do until to test (upcase(var1)='A') I get the following
70 data b;
71 do until (upcase(var1)='A');
72 set a;
ERROR: Variable var1 has been defined as both character and numeric.
NOTE: Numeric values have been converted to character values at the places
NOTE: The SAS System stopped processing this step because of errors.
I understand that SAS determines the type and length of a variable during
compile time at the first reference to the variable. In my first example
SAS makes var1 character with length 1 (type and length being determined by
the comparison to a character string of length 1). Apparently when the
variable is an argument to a function, SAS defines it as numeric, even if it
is a character function and being compared to a character string.
Of course the fix (and better practice in general), is to add a length
statement at the top to define both the type and length of the var1, but I
thought this was interesting.