DATA Step, Macro, Functions and more

SCAN FUNCTION AND LENGTH

Reply
Contributor
Posts: 29

SCAN FUNCTION AND LENGTH

Hi everybody,

 

my name is Constantin. I need help with the subject above. 

I dont get it. The manual says

"In a DATA step, if the SCAN function returns a value to a variable that has not yet been given a length, then that variable is given a length of 200 characters. If you need the SCAN function to assign to a variable a word that is longer than 200 characters, then you should explicitly specify the length of that variable".

 

Ok. I did try it, but it didnt work.

 

Example:

DATA data;

name=scan('Mr Smith',2);

RUN;

 

I get the length 8. Please, help me.

 

Bye.

Super User
Posts: 5,509

Re: SCAN FUNCTION AND LENGTH

The manual is wrong here.

 

There are many functions that return character strings, and for many of those the manual would be correct.  (For many additional character functions, the new variable would be assigned a length equal to the length of the incoming string.)  But SCAN returns a number, not a character string.  The default length for numbers is 8.

Respected Advisor
Posts: 3,799

Re: SCAN FUNCTION AND LENGTH

Posted in reply to Astounding

Are you sure about that?

 


Astounding wrote:

The manual is wrong here.

 

There are many functions that return character strings, and for many of those the manual would be correct.  (For many additional character functions, the new variable would be assigned a length equal to the length of the incoming string.)  But SCAN returns a number, not a character string.  The default length for numbers is 8.


 

Super User
Posts: 5,509

Re: SCAN FUNCTION AND LENGTH

Posted in reply to data_null__

No, my mistake.  I was mixing together two functions.  SCAN definitely returns a character string, not a number.

 

If you think about the result, this behavior makes sense.  SAS has to decide on a length for the new variable before it actually examines the data.  It is possible that SCAN will retrieve the entire incoming string, so to be safe it assigns the new variable the length of the incoming string.  Same behavior for SUBSTR.

Trusted Advisor
Posts: 1,117

Re: SCAN FUNCTION AND LENGTH

Hi @cons,

 

The SAS 9.4 documentation of the SCAN function says:

"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."

 

So, I guess, you're using SAS 9.4, but you read the documentation of an earlier SAS version. The string "Mr Smith" has a length of 8 characters, hence the length of variable NAME.

Contributor
Posts: 29

Re: SCAN FUNCTION AND LENGTH

Posted in reply to FreelanceReinhard
Thank u great job FreelanceReinhard
Ask a Question
Discussion stats
  • 5 replies
  • 1079 views
  • 0 likes
  • 4 in conversation