DATA Step, Macro, Functions and more

length of variable when using substr(scan())

Reply
Occasional Contributor
Posts: 19

length of variable when using substr(scan())

Hi,

 

Below is the program:

 

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

Can you tell me why the length of first is returned as 200 even when author has 15....??

 

Super User
Posts: 2,049

Re: length of variable when using substr(scan())

[ Edited ]

The Length of the Result
In a DATA step, most variables have a fixed length. If the word returned by the SCAN function is assigned to a variable that has a fixed length greater than the length of the returned word, then the value of that variable will be padded with blanks. Macro variables have varying lengths and are not padded with blanks.
The maximum length of the word that is returned by the SCAN function depends on the environment from which it is called:
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.
If you use the SCAN function in an expression that contains operators or other functions, a word that is returned by the SCAN function can have a length of up to 32,767 characters, except in a WHERE clause. In that case, the maximum length is 200 characters.
In the SQL procedure, or in a WHERE clause in any procedure, the maximum length of a word that is returned by the SCAN function is 200 characters.

Valued Guide
Posts: 596

Re: length of variable when using substr(scan())

I would add that I think it's good practice to always assign a length to character variables with the LENGTH statement - you can save yourself a lot of pain by doing that. Numeric variables will always have a default length of 8 which is fine so most people won't bother declaring them.
Super User
Super User
Posts: 8,272

Re: length of variable when using substr(scan())

[ Edited ]

ab12nov wrote:

Hi,

 

Below is the program:

 

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

Can you tell me why the length of first is returned as 200 even when author has 15....??

 


Because SAS cannot read your mind, although SAS 9.4 seems to know that 15 is the max that function will generate.

Note that there are functions like FIRST() and CHAR() that SAS knows will only return one character.

But if you want FIRST to be one character long then define it that way.  And if you did define it that way then you don't really need SUBSTR() function anymore.

data test;
    Author = 'Agatha Christie';
    First1 = substr(scan(author,1,' ,'),1,1);
    First2 = first(scan(author,1,' ,'));
    First3 = char(scan(author,1,' ,'),1);
    length first $1 ;
    first= scan(author,1,' ,') ;
run;
proc contents; run;
proc print; run;

image.png

 

Ask a Question
Discussion stats
  • 3 replies
  • 383 views
  • 0 likes
  • 4 in conversation