SAS converts character variable to numeric without asking
I have the following SAS code:
DATA WORK.temp;
SET WORK.proj;
RETAIN prev_time
prev_name
prev_type
count 0;
FLAG = 0;
IF( (prev_name = name) AND (prev_type NE type) AND (prev_type = 'x')) THEN DO;
TimeDifference = time - prev_time;
IF( TimeDifference < 10*60) THEN DO;
FLAG = 1;
count + 1;
END;
END;
prev_time = time;
prev_type = type;
prev_name = name;
RUN;
When I run the program, I get records telling me that my character values have been converted to numeric values:
NOTE. Character values have been converted to numeric values
NOTE. Invalid numeric data, name = 'somename'
NOTE. Invalid numeric data, name = 'othername'
I've never asked SAS to do this conversion and it causes errors in the output. Any idea what is causing this?
Thank!
source to share
You certainly asked SAS (implicitly).
RETAIN prev_time
prev_name
prev_type
count 0;
It doesn't just set count
to 0. It sets all four variables to 0
, which is numeric. So, prev_name
is numeric, and when you say prev_name=name
(both in the statement IF
and the assignment later), you get this note.
You can see it here:
data test;
retain x y z 0;
put x= y= z=;
run;
All three variables are initialized to 0, not just z
. The newline in your code doesn't make any difference (as it usually does in SAS).
Also, your variable is count
automatically saved using it the way you did:
count+1;
The sum operator preserves, AND works like sum()
, which means that the missing plus one is one (not missing).
So the simplest solution here is to remove count 0
from the save statement and the rest should be fine as it is.
source to share