BookmarkSubscribeRSS Feed
ab12nov
Obsidian | Level 7

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

 

3 REPLIES 3
novinosrin
Tourmaline | Level 20

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.

ChrisBrooks
Ammonite | Level 13
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.
Tom
Super User Tom
Super User

@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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 3765 views
  • 0 likes
  • 4 in conversation