3

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 notes 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 never asked SAS to make this conversion, and it is causing errors in the output. Any idea what is causing this?

Thanks!

1 Answer 1

3

You most certainly did ask SAS to (implicitly).

RETAIN prev_time
       prev_name
       prev_type
       count 0;

That 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 IF statement and the assignment later) you get this note.

You can see this 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 have any meaning (as is normal in SAS).

Further, your count variable is automatically retained by using it in the fashion you did:

count+1;

The sum operator retains, AND works like sum(), which means that missing plus one is one (not missing).

So the simplest fix here is to remove count 0 from the retain statement, and the rest should be fine as is.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Joe. I had read the examples here: support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/…, but now realize that I misinterpreted what they were saying. I'm guessing the fix is to put the count variable first? Extra points for being so speedy!
You would need two retain statements, or to remove the 0 intialization in the retain statement. As it stands, you can remove the count variable from the retain entirely - it is automatically retained and not harmed by starting out missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.