hyg17
hyg17

Reputation: 237

SUBSTR(SCAN(...)) and its contents

I was asked to find the length of First defined in the program below and I think it should be 15 but the answer says it is 200.

My thought is that SCAN(Author,1,',') has value Agatha, but since I did not define its length the length is as same as Author, which is 15. Again, First is valued as A since its the first letter of Agatha, but because of not specifying the length is still 15.

data test;
    Author='Agatha Christie';
    First=substr(scan(Author,1,','),1,1);
run;

proc contents;
run;

I have no clue where 200 comes from... can I get some help?

Upvotes: 0

Views: 535

Answers (2)

Kiran
Kiran

Reputation: 3315

Before SAS 9.4, the variable length returned from Scan function used to be 200. But SAS 9.4 variable length give in the scan function is same as the variable it operates on. hence new variable length will be 15 not 200 as said by @Craig. below is information for SAS 9.4 documentation.

In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, that variable is given the length of the first argument. This behavior is different from the behavior in previous releases of SAS. In previous releases, code that created a variable with a length of 200 might have produced a variable with a length that was greater than expected. If you need the SCAN function to assign a variable with a value that is different from the length of the first argument, use a LENGTH statement.

Upvotes: 5

Craig Srsen
Craig Srsen

Reputation: 475

The answer is wrong. It's 15. x

Upvotes: 0

Related Questions