|
Hi All,
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
or numeric.
If I code the below simple DOW-loop everything is fine.
data a;
input Var1 $1;
cards;
x
A
x
a
;
run;
data b;
do until (var1='A');
set a;
end;
run;
But if I change the do until to test (upcase(var1)='A') I get the following
error:
70 data b;
71 do until (upcase(var1)='A');
72 set a;
ERROR: Variable var1 has been defined as both character and numeric.
73 end;
74 run;
NOTE: Numeric values have been converted to character values at the places
given by:
(Line):(Column).
71:20
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.
Kind Regards,
--Quentin
|