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....??
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.
@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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.